使用ServletActionContext访问Servlet API
2014-12-23 00:00
429 查看
LoginAction.java
package org.sadhu.app.action; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import javax.servlet.http.Cookie; import org.apache.struts2.ServletActionContext; /* * 使用ServletActionContext访问Servlet API * 利用此对象的话就不用实现XxxAware接口,但是会与Servlet API直接耦合,不利于高层次的解耦。 * */ public class LoginAction implements Action { private String username; private String password; 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 String execute() throws Exception { ActionContext ctx = ActionContext.getContext(); Integer counter = (Integer)ctx.getApplication().get("counter"); if(counter == null) { counter = 1; } else { counter++; } //通过ActionContext设置application范围的属性 ctx.getApplication().put("counter", counter); //通过ActionContext设置session范围的属性 ctx.getSession().put("user",getUsername()); if(getUsername().equals("sadhu") && getPassword().equals("sadhu")) { Cookie c = new Cookie("user",getUsername()); c.setMaxAge(60*60); //直接获得response对象然后使用。 ServletActionContext.getResponse().addCookie(c); //通过ActionContext设置request范围的属性 ctx.put("tip","服务器提示,您已经成功的登陆"); return SUCCESS; } else { ctx.put("tip", "服务器提示,登陆失败"); return ERROR; } } }
相关文章推荐
- Struts2(6):使用ServletActionContext访问Servlet API
- 3.5.4: 使用ServletActionContext访问Servlet API
- Action使用ActionContext访问Servlet API
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- ServletActionContext访问Servlet API
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- 使用struts2框架后servlet不能正常访问的解决方案 There is no Action mapped for action name null
- 使用ActionContext类访问Servlet API
- Struts2_008_Servlet API耦合的访问方式值之ServletActionContext
- struts2_day02_06-获取表单数据(使用ServletActionContext获取和接口注入)
- Struts2 访问Servlet API(ActionContext)以及校验框架
- eclipse使用ServletActionContext.getRequest总是出错的解決方法
- Action访问Servlet API---ActionContext类
- idea编辑器HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath();方法无法使用
- struts2的ActionContext与 ServletActionContext区别与使用场景
- eclipse使用ServletActionContext.getRequest总是出错的解決方法