Struts2(6):使用ServletActionContext访问Servlet API
2013-06-23 02:56
344 查看
除此之外,为了能直接访问Servlet API,Struts2还提供了一个ServletActionContext工具类,这个工具类包含了如下几个静态方法:
ServletActionContext的几个静态方法
借助于ServletActionContext类的帮助,开发者也可以在Action中访问Servlet API,并可避免Action类需要实现XxxAware接口---虽然如此,但该Action依然与Servlet API直接耦合,不利于高层次的解耦。
借助于ServletActionContext工具类的帮助,Action能以更简单的方式来访问Servlet API:
方法 | 说明 |
static PageContext getPageContext( ) | 取得Web应用的PageContext对象 |
static HttpServletRequest getRequest( ) | 取得Web应用的HttpServletRequest对象 |
static HttpServletResponse getResponse( ) | 取得Web应用的HttpServletResponse对象 |
static ServletContext getServletContext( ) | 取得Web应用的ServletContext对象 |
借助于ServletActionContext工具类的帮助,Action能以更简单的方式来访问Servlet API:
public class Login { 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(){ if(getUsername().equals("scott")&& getPassword().equals("tiger")){ HttpSession session=ServletActionContext.getRequest().getSession(); session.setAttribute("username",getUsername()); return "success"; } return "login"; } }
相关文章推荐
- 3.5.4: 使用ServletActionContext访问Servlet API
- 使用ServletActionContext访问Servlet API
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- Struts2_008_Servlet API耦合的访问方式值之ServletActionContext
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- struts2的ActionContext与 ServletActionContext区别与使用场景
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- Struts2中访问Servlet中的API(ActionContext,ServletActionContext)两种方式
- Action使用ActionContext访问Servlet API
- struts2_day02_06-获取表单数据(使用ServletActionContext获取和接口注入)
- ServletActionContext访问Servlet API
- struts2中 ServletActionContext与ActionContext区别
- struts2 Action访问Servlet Api
- Struts2(5):Action中直接访问Servlet API---实现XxxAware接口
- struts2中 ServletActionContext与ActionContext区别
- Struts2中解耦的方式访问appliactionContext对象
- struts2 action空指针异常 org.apache.struts2.ServletActionContext java.lang.NullPointerException