| 发表于:2007-07-22 22:08:121楼 得分:0 |
补充action文件 loginaction.java package ems.emsbean.login; import org.apache.struts.action.actionmapping; import org.apache.struts.action.actionform; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import org.apache.struts.action.actionforward; import org.apache.struts.action.action; import ems.emsbean.hibernate.user; import net.sf.hibernate.hibernateexception; import net.sf.hibernate.query; import net.sf.hibernate.session; import ems.emsbean.login.loginactionform; import ems.emsbean.hibernate.hibernateutil; import org.apache.struts.action.actionmessages; import org.apache.struts.action.actionmessage; public class loginaction extends action { public actionforward EXECute(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) { httpsession session = request.getsession(); session s = hibernateutil.currentsession(); loginactionform loginactionform = (loginactionform) form; string utype =loginactionform.getutype(); string uname =loginactionform.getuname(); string pwd =loginactionform.getpwd(); string[] userlist = new string[2]; int loginutype = integer.parseint(utype); userlist[0] = uname; userlist[1] = pwd; try { hibernateutil.begintransaction(); string str = new string(); switch (loginutype) { case 1: str = " from user where user.name=:stuname and user.password=:stupassword "; query query = s.createquery(str); // system.out.println(username + " " + password); query.setstring( "stuname ", uname); query.setstring( "stupassword ", pwd); if (query.list().size() > 0) { session.setattribute( "id ", ((user) query.list().get(0)).getid()); s.close(); return mapping.findforward( "home "); } else break; case 2: str = " from user where user.name = ' " + uname + " ' and user.password = ' " + pwd + " ' "; if (s.createquery(str).list().size() > 0) { // session.setattribute( "teaid ", ((teacher) s.createquery(str) // .list().get(0)).getid()); s.close(); return mapping.findforward( "admin "); } else break; default: break; } } catch (hibernateexception e) { e.printstacktrace(); } finally { try { s.close(); } catch (hibernateexception ex) { } } actionmessages errors = new actionmessages(); errors.add( "login error ", new actionmessage( "login.error ")); saveerrors(request, errors); return mapping.getinputforward(); } } | | |
|