LoginService:
package com.service.login;
import java.util.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.util.*;
import com.model.*;
public class LoginService {
private HibernateServiceTemplate hibernateServiceTemplate;
public boolean login(TBussinessPos user) {
boolean ret = false;
try {
// 1身份验证
ret = this.authenticate(user);
if(!ret){
return ret;
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* 身份验证
*
* @param user
* @return
*/
public boolean authenticate(TBussinessPos user) {
boolean ret = false;
try {
String hql = " from TBussinessPos where windowNo=? and password=? ";
List<Object> params = new ArrayList<Object>();
params.add(user.getWindowNo());
params.add(StringUtil.encryptStringBymoveByte(user.getPassword()));
// 从列表中0开始取,取一条数据
List<TBussinessPos> users = this.hibernateServiceTemplate.find(hql,
params,0,1);
if (users != null && users.size() > 0) {
ret = true;// 登录成功
TBussinessPos u = users.get(0);
//把u放入Session中
ActionContext.getContext().getSession().put("login_user", u);
user.setSn(u.getSn());
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public HibernateServiceTemplate getHibernateServiceTemplate() {
return hibernateServiceTemplate;
}
public void setHibernateServiceTemplate(
HibernateServiceTemplate hibernateServiceTemplate) {
this.hibernateServiceTemplate = hibernateServiceTemplate;
}
}
LoginAction:
package com.action.login;
import com.util.*;
import com.service.login.*;
import com.model.*;
public class LoginAction extends CommonAction{
private LoginService loginService;
private TBussinessPos user;
public String login(){
if(this.loginService.login(user)){
return SUCCESS;
}
return ERROR;
}
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public TBussinessPos getUser() {
return user;
}
public void setUser(TBussinessPos user) {
this.user = user;
}
}
login.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="login" extends="struts-default">
<action name="login" class="loginAction" method="login">
<result name="success" type="redirect">/main.jsp</result>
<result name="error" type="redirect">/index.jsp</result>
</action>
</package>
</struts>