struts2中提供了Map类型的request,session,与application,可以从ActionContext对象中获得.
(1)实例ActionContext,通常通过ActionContext对象提供的getContext()方法获取
public static ActionContext getContext(),该方法是静态的,可以直接调用
ActionContext=ActionContext.getContext()
(2)获取Map类型的request
Map request=ActionContext.getContext().get("request");
(3)获取Map类型的session
Map request=ActionContext.getContext().getSession();
(4)获取Map类型的application
Map request=ActionContext.getContext().getApplication();
public class LoginAction { private String username; private String password; public String login(){ return "login"; } public String logined(){ if(StringUtil.isNotEmpty(username)&&StringUtil.isNotEmpty(password)){ ActionContext.getContext().getSession().put("username",username); return "loginSuccess"; }else{ return "error"; } } //生成setter和getter 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; } }
虽然在struts2中使用ActionContext来访问 Servlet API,但这种方法不能直接获得ServletAPI实例,为了再action中直接访问 Servlet API可以通过以下方法直接访问
(1)ServletContextAware:实现该接口的Action可以直接访问Web应用的ServletContext实例
(2)ServletRequestAware:实现该接口的Action可以直接访问Web应用的HttpServletRequest实例
(3)ServletResponseAware:实现该接口的Action可以直接访问Web应用的HttpServletResponse实例
public class LoginAction implements ServletResponseAware{ private String username; private String password; private HttpServletResponse response; public String login(){ return "login"; } public String logined(){ //记录一个网站访问次数 ActionContext context=ActionContext.getContext(); Integer count=(Integer)context.getApplication().get("count"); if(count==null){ count=1; }else{ count+=1; } context.getApplication().put("count", count); if(StringUtil.isNotEmpty(username)&&StringUtil.isNotEmpty(password)){ Cookie cookie=new Cookie("username", username); cookie.setMaxAge(60*60); response.addCookie(cookie); context.getSession().put("username",username); return "loginSuccess"; }else{ return "error"; } } //生成setter和getter 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; } @Override public void setServletResponse(HttpServletResponse response) { this.response=response; } }
除上述两种方法外,还可以通过ServletActionContext访问Servlet API
public class LoginAction{ private String username; private String password; private HttpServletResponse response; public String login(){ return "login"; } public String logined(){ //记录一个网站访问次数 ActionContext context=ActionContext.getContext(); Integer count=(Integer)context.getApplication().get("count"); if(count==null){ count=1; }else{ count+=1; } context.getApplication().put("count", count); if(StringUtil.isNotEmpty(username)&&StringUtil.isNotEmpty(password)){ Cookie cookie=new Cookie("username", username); cookie.setMaxAge(60*60); ServletActionContext.getResponse().addCookie(cookie); context.getSession().put("username",username); return "loginSuccess"; }else{ return "error"; } } //生成setter和getter 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; } }