📄 loginaction.java
字号:
package com.stuman.web.struts.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.stuman.dao.hibernate.HibernateUtil;
import com.stuman.domain.Admin;
import com.stuman.domain.Student;
import com.stuman.domain.Teacher;
import com.stuman.web.struts.form.LoginForm;
/**
* @author 张健霖
*
*/
public class LoginAction extends Action {
/** actionforward 用来处理http的request response
* Process the specified HTTP request, and create the corresponding HTTP response
* (or forward to another web component that will create it), with provision for handling
* exceptions thrown by the business logic.
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException {
HttpSession session = request.getSession();
LoginForm loginForm = (LoginForm) form;
Session s = HibernateUtil.currentSession();
String sort = loginForm.getSort();
String username = loginForm.getUsername();
String password = loginForm.getPassword();
int loginSort = Integer.parseInt(sort);
String[] userlist = new String[2];
userlist[0] = username;
userlist[1] = password;
try {
HibernateUtil.beginTransaction();
String str = new String();
switch (loginSort) {
case 1:
str = " from Student as stu where stu.name=:stuName and stu.password=:stuPassword";
Query query = s.createQuery(str);
System.out.println(username + " " + password);
query.setString("stuName", username);
query.setString("stuPassword", password);
// list()函数用来查询
if (query.list().size() > 0) {
//保存stuid到session中
session.setAttribute("stuid", ((Student) query.list().get(0)).getId());
//s.close();
return mapping.findForward("studentLoginsuccess");
} else
break;
case 2:
// 另外的一种查询语句
str = " from Teacher tea where tea.name = '" + username
+ "' and tea.password ='" + password + "'";
if (s.createQuery(str).list().size() > 0) {
session.setAttribute("teaid", ((Teacher) s.createQuery(str)
.list().get(0)).getId());
//s.close();
return mapping.findForward("teacherLoginsuccess");
} else
break;
case 3:
str = " from Admin admin where admin.name = '" + username
+ "' and admin.password ='" + password + "'";
System.out.println(username + " " + password);
if (s.createQuery(str).list().size() > 0) {
session.setAttribute("id", ((Admin) s.createQuery(str)
.list().get(0)).getId());
s.close();
System.out.println("adminLogin");
return mapping.findForward("adminLoginsuccess");
} else
break;
default:
break;
}
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("here have problem");
} finally {
System.out.println("here have problem too can't reach mapping.findForward");
s.close();
}
ActionMessages errors = new ActionMessages();
errors.add("login error", new ActionMessage("login.error"));
saveErrors(request, errors);
return mapping.getInputForward();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -