Struts2中Action访问Servlet API的两种方法


Struts2的Action并未直接与任何Servlet  API偶合,这也是Struts2的一个改良的地方。但如何进行访问?


方法一:.[一般推荐使用](只能获得request,而response则得不到)
Struts2提供了一个ActionContext类,Struts2中的Action可以通过它进行访问。
其方法有:get(),getApplication(),getContext(),getParameters(),getSession(),setApplication(),setSession()
  1. /**
     * @作者 Jcuckoo
     * @创建日期 2008-12-02
     * @版本 V 1.0
     */
  2. public class LoginAction implements Action
  3. {
  4.     private String username;
  5.     private String password;
  6.     public String getPassword()
  7.     {
  8.         return password;
  9.     }
  10.     public void setPassword(String password)
  11.     {
  12.         this.password = password;
  13.     }
  14.     public String getUsername()
  15.     {
  16.         return username;
  17.     }
  18.     public void setUsername(String username)
  19.     {
  20.         this.username = username;
  21.     }
  22.     public String execute() throws Exception
  23.     {
  24.         //获取静态方法,获取系统的ActionContext实例
  25.             ActionContext ctx = ActionContext.getContext();
  26.         //获取servletContext里的counter属性
  27.         Integer counter = (Integer)ctx.getApplication().get("counter");
  28.         if (counter == null)
  29.         {
  30.             counter = 1;
  31.         }
  32.         else
  33.         {
  34.             counter = counter + 1;
  35.         }
  36.         //将增加1后的counter值设置成counter属性
  37.         ctx.getApplication().put("counter" , counter);
  38.         ctx.getSession().put("user" , getUsername());
  39.         if (getUsername().equals("scott")&& getPassword().equals("tiger") )
  40.         {
  41.         //直接设置HttpServletRequest属性
  42.             ctx.put("tip" , "服务器提示:您已经成功的登陆");
  43.                     return SUCCESS;
  44.         }else{
  45.         //直接设置HttpServletRequest属性
  46.             ctx.put("tip" , "服务器提示:登陆失败");
  47.                  return ERROR;
  48.         }
  49.     }
  50. }
方法二:[不推荐](麻烦,与servlet API 耦合大).

虽然Struts2提供了ActionContext来访问Servlet API,但这种访问毕竟不能直接获得Servlet API,为了在Action中直接访问Servlet API,Struts2还提供了一下接口:ServletContextAware,ServletRequestAware,ServletResponseAware
下面以ServletResponseAware为例。
  1. /**
     * @作者 Jcuckoo
     * @创建日期 2008-12-02
     * @版本 V 1.0
     */
  2. //实现ServletResponseAware
  3. public class LoginAction implements Action , ServletResponseAware
  4. {
  5. //需要访问的HttpServletResponse对象
  6.     private HttpServletResponse response;
  7.     private String username;
  8.     private String password;
  9.     public String getPassword(){
  10.         return password;
  11.     }
  12.     public void setPassword(String password){
  13.         this.password = password;
  14.     }
  15.     public String getUsername(){
  16.         return username;
  17.     }
  18.     public void setUsername(String username){
  19.         this.username = username;
  20.     }
  21. //实现ServletResponseAware接口必须实现的方法
  22.     public void setServletResponse(HttpServletResponse response) {
  23.     this.response = response;
  24.   }
  25.     public String execute() throws Exception{
  26.         Cookie c = new Cookie("user" , getUsername());
  27.         c.setMaxAge(60 * 60);
  28.         response.addCookie(c);
  29.         return SUCCESS;
  30.     }
  31. }
方法三:强烈推荐使用

Struts2还提供了一个ServletActionContext,其静态方法有:getPageContext(),getRequest(),getResponse(),getServletContext()
  1. HttpServletRequest request = ServletActionContext.getRequest(); 
  2. HttpServletResponse response = ServletActionContext.getResponse(); 
  3. request.getSession().setAttribute("username","admin"); 
  4. request.setAttribute("password""1234");


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值