废料 Struts2获取request三种方法

复习啊..

     struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象。
  • 在Action中获取request方法一:
 
在Action中的代码:
?
1
2
3
Map request = (Map)ActionContext.getContext().get( "request" );
List<Task> tasks = taskManager.findAll();
request.put( "tasks" , tasks);


 
在JSP页面中获取其中的值:

?
1
2
3
4
5
6
7
8
9
10
< s:iterator id = "task" value = "#request.tasks" >
        < tr class = "table_header" >
         < td >< s:property value = "#task.tname" /></ td >
         < td >< s:property value = "#task.tuid" /></ td >
         < td >< s:property value = "#task.tstartTime" /></ td >
         < td >< s:property value = "#task.tendTime" /></ td >
         < td >< s:property value = "#task.tstate" /></ td >
         < td >< input type = "radio" id = "choose" name = "choose" onclick = "getId(this.value)" value="<s:property value = '#task.tid' />"/></ td >
        </ tr >
</ s:iterator >


--------------------------------------------------------------------------------------------
  • 方法二:通过ServletActionContext类来获取,使用struts2经验如果处理get传参是中文,只能使用该方法进行处理乱码问题
 
Action中代码:
?
1
2
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute( "username" , "zhangsan" );


 
在jsp中获取其中的值
 
?
1
< s:property value = "#request.username" >或者${requestScope.req}

-------------------------------------------------------------------------------------------- 
  • 方法三:通过ServletRequestAware接口通过IOC机制注入Request对象
Action中的代码:
Action实现ServletRequestAware接口,实现接口中的方法
     
?
1
2
3
4
5
6
7
8
9
10
private HttpServletRequest request;
      //实现接口中的方法
      public void setServletRequest(HttpServletRequest request){
       this .request = request;
      }
      //然后在execute()方法中就可以使用了
      public String execute(){
       request.setAttribute( "username" , "zhangsan" );
       request.getSession().getServletContext().getApplication(); //得到Application
      }

     该方法必须要实现,而且该方法是自动被调用
     这个方法在被调用的过程中,会将创建好的request对象通过参数的方式传递给你,你可以用来赋给你本类中的变量,然后request就可以使用了
       注意:setServletRequest()方法一定会再execute()方法被调用前执行
 
在jsp页面中获取其中的值
?
1
< s:property value = "#request.task.tname" />

1、浏览器往Action传递参数:

    在Action中设置属性,并为属性设置get/set方法。传递参数时按照属性的名字赋值即可。如xxx?name=aa

    取值用request.getPrameters("属性名“);

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UserAction</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span>
<span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> name</span><span class="pun">;</span><span class="pln">  </span>
<span class="pln">    </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">User</span><span class="pln"> user</span><span class="pun">;</span><span class="pln">  </span>
<span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> userAdd</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span>
<span class="pln">        </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">user</span><span class="pun">.</span><span class="pln">getName</span><span class="pun">());</span><span class="pln">  </span>
<span class="pln">        </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">name</span><span class="pun">);</span><span class="pln">  </span>
<span class="pln">        </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"success"</span><span class="pun">;</span><span class="pln">  </span>
<span class="pln">    </span><span class="pun">}</span><span class="pln">  </span>
<span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> getName</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span>
<span class="pln">        </span><span class="kwd">return</span><span class="pln"> name</span><span class="pun">;</span><span class="pln">  </span>
<span class="pln">    </span><span class="pun">}</span><span class="pln">  </span>
<span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setName</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> name</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span>
<span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">name </span><span class="pun">=</span><span class="pln"> name</span><span class="pun">;</span><span class="pln">  </span>
<span class="pln">    </span><span class="pun">}</span><span class="pln">  </span>
<span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">User</span><span class="pln"> getUser</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span>
<span class="pln">        </span><span class="kwd">return</span><span class="pln"> user</span><span class="pun">;</span><span class="pln">  </span>
<span class="pln">    </span><span class="pun">}</span><span class="pln">  </span>
<span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setUser</span><span class="pun">(</span><span class="typ">User</span><span class="pln"> user</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span>
<span class="pln">        </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">user </span><span class="pun">=</span><span class="pln"> user</span><span class="pun">;</span><span class="pln">  </span>
<span class="pln">    </span><span class="pun">}</span><span class="pln">  </span>
<span class="pun">}</span><span class="pln">  </span>

注:struts2不会使用单例模式,因此每次的请求都是new 一个新对象。


2、Action 往浏览器界面传递参数:

     方式有三种:

    1)种:直接给Action 属性(有get/set方法)赋值 ,JSP中用

  1. <s:property value="OGNL表达式"/> 取值,注意返回结果类型为forward  

    例如:

  1. <s:property value="name"/>  
  2. <s:property value="user.name"/>  

     2)种:通过ActionContext传值,在Action中所调用的方法中加入:

  1. <p><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;"><span class="typ">ActionContext</span><span class="pun">.</span><span class="pln">getContext</span><span class="pun">().</span><span class="pln">put</span><span class="pun">(</span></span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;">"key"</span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;"><span class="pun">,</span><span class="pln"> </span></span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;">"value"</span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;"><span class="pun">);</span><span class="pln">  </span></span></p>

      JSP中用

  1.  <s:property value="#key"/>取值 

    

    3)种:通过request、session 传值。Action方法中通过取得HttpServletRequest 、HttpSession 和 Map对象设置值,

例如:

  1. <p><span class="typ">ServletActionContext</span><span class="pun">.</span><span class="pln">getRequest</span><span class="pun">().</span><span class="pln">setAttribute</span><span class="pun">(</span><span class="str">"arg0"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">);</span><span class="pln">  </span>
    <span class="typ">ServletActionContext</span><span class="pun">.</span><span class="pln">getRequest</span><span class="pun">().</span><span class="pln">getSession</span><span class="pun">().</span><span class="pln">setAttribute</span><span class="pun">(</span><span class="str">"arg0"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">);</span><span class="pln">  </span>
    <span class="typ">ActionContext</span><span class="pun">.</span><span class="pln">getContext</span><span class="pun">().</span><span class="pln">getSession</span><span class="pun">().</span><span class="pln">put</span><span class="pun">(</span><span class="str">"key"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">);</span><span class="pln">  </span></p>

jsp页面通过:

废料 Struts2获取request三种方法_第1张图片


【转自】 http://cailangwei.blog.163.com/blog/static/131458579201287112552485/



在上面的代码中, 在Action实现了一个 ServletRequestAware接口,并且实现了 setServletRequest方法。

如果一个动作类实现了 ServletRequestAware接口, Struts2在调用 execute方法之前,就会先调用setServletRequest方法,并将Request参数传入这个方法。如果想获得 HttpServletResponse、HttpSession和 Cookie等对象,动作类可以分别实现 ServletResponseAware、 SessionAware和CookiesAware等接口。这些接口都在 org.apache.struts2.interceptor包中。

struts2超链接传值: <s:a href="info.action?id=%{#list.id}"> <s:property value="#list.title"/></s:a>

你可能感兴趣的:(废料 Struts2获取request三种方法)