如何在struts2的Action中如何获得ServletContext 服务器容器对象
1.实现接口(import org.apache.struts2.util.ServletContextAware;) implements ServletContextAware
2.定义变量
/**
* 服务器容器
*/
private ServletContext context;
3.实现方法
public void setServletContext(ServletContext arg0) {
this.context = arg0;
}
4.具体代码应用(将登陆用户ID,放入服务器容器)
/**
* 判断用户账号密码是否正确,是否已经登入
*/
@SuppressWarnings("unchecked")
private boolean isgo() {
// 调用service将用户信息调出
TEmployeeSafe tem = empSafeService.queryById(employeeId);
if (tem == null) {
this.setMsg("用户名输入有误!");
return false;
}
// 新增验证,验证当前工号对应角色是否为开启状态
// t_employee_safe.roleNum = t_employee_role.roleNum 并且t_employee_role.state = 10
//TEmployeeRole role = service.queryByRoleNum(tem);
TEmployeeRole role = empRoleService.queryById(tem.getRoleNum());
if(role.getState() != null){
if(!role.getState().equals("10")){
this.setMsg("当前用户对应角色未开启!");
return false;
}
}else{
this.setMsg("当前用户无对应角色!");
return false;
}
TEmployeeInfo info = empService.queryEmploye(employeeId);
// 将密码转换为md5 形式
String pwd = KeyedDigestMD5.getKeyedDigest(employeePass, "").toUpperCase();
if (tem.getEmployeePass().equals(pwd)) {
// 取得服务器中的用户集合
HashMap<String, String> map = (HashMap<String, String>) context
.getAttribute("userList");
// 获取sessionId
String SessionId = ServletActionContext.getRequest().getSession()
.getId();
// 如果服务器中不存在此用户则将用户加入集合,存在则删除原来的,再重新加入新的。
if (map == null || map.size() == 0) {
map = new HashMap<String, String>();
// 创立标志
map.put(tem.getEmployeeId(), SessionId);
// 将标志放入服务器中
context.setAttribute("userList", map);
// 将标志放入session
ServletActionContext.getRequest().getSession().setAttribute(
"user", tem);
ServletActionContext.getRequest().getSession().setAttribute(
"info", info);
ServletActionContext.getRequest().getSession().setAttribute(
"role", role);
} else {
if (map.get(tem.getEmployeeId()) != null) {
// 存在用户则将其删除
map.remove(tem.getEmployeeId());
}
map.put(tem.getEmployeeId(), SessionId);
// 将标志重新放入服务器中
context.setAttribute("userList", map);
// 将标志放入session
ServletActionContext.getRequest().getSession().setAttribute(
"user", tem);
}
return true;
}
this.setMsg("密码输入有误!");
return false;
}
==================================================================================
5.导包时注意,必须导入import org.apache.struts2.util.ServletContextAware;
strurts2 application 报错
package com.bonck.user.login;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.struts.ActionSupport;
import com.bonck.app.UserApp;
import com.bonck.dao.Useinfo;
public class LoginAction extends ActionSupport implements ServletContextAware,
ServletRequestAware {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private HttpServletRequest request;
private ServletContext application;
public String execute() throws Exception {
UserApp app=new UserApp();
Useinfo user =app.getUser(username, password);
if(user.getUiId()!=null){
request.setAttribute("username", username);
application.setAttribute("pwd", password);
return "welcome";
}
else
return "error";
}
public void setServletContext(ServletContext arg0) {
application = arg0;
}
public void setServletRequest(HttpServletRequest arg0) {
request = arg0;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public ServletContext getApplication() {
return application;
}
public void setApplication(ServletContext application) {
this.application = application;
}
}
一直报错,最后终于发现spring倒的鬼,解决方案:将该
import org.springframework.web.context.ServletContextAware;
换成
import org.apache.struts2.util.ServletContextAware;即可
看来还的认真!