Struts2系列:(22) result 和OGNL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2系列:(22) result 和OGNL相关的知识,希望对你有一定的参考价值。

简单的描述一下OGNL和result的作用:

(1)通过OGNL可以从ActionContext或ValueStack上获取值。

(2)对于result,在struts.xml中,可以为action配置不同的处理结果。


本文关注2两个问题:(1)在struts.xml文件中,使用OGNL为result提供dynamic parameter;(2)redirect和redirectAction之间的区别。


1、使用OGNL为result提供dynamic parameter

在struts.xml中,常用的result type有三种类型:dispatcher、redirect和redirectAction。

这里主要是讨论redirect和redirectAction类型中使用OGNL。


原因是这样的:dispatcher,本质上是使用了RequestDispatcher;而RequestDispatcher是属于Servlet的API,RequestDispatcher有一个forward()方法,它的功能是将第一个Servlet控制权转交给第二个Servlet,在这里要注意的是第一个Servlet和第二个Servlet有一个共同点,就是它们处理的request和response是同一对象,因此它们处理的数据(或接收到参数)是一样的或者说是能共享的;在struts2的Action代码类中,它所能接触到request、response、ActionContext、ValueStack等数据,和进行dispatcher之后的JSP(本质上是Servlet)是可以共享的,换句话说,在dispatcher之后JSP是可以通过OGNL在页面当中拿到ActionContext和ValueStack上的值,因此不需要为dispatcher类型的result类型提供dynamic parameter。


但是,我们可以为dispatcher类型的result提供dynamic parameter,只是没有必要而已。

而redirect和redirectAction它们前后处理的request和response是不同的对象了,因此在这种情况下,如果第一个Action想给后面重定向的地址传递参数,就需要通过queryString进行传递,当然也可以通过Session进行传递,但是不建议用Session传递数据,因为每个用户都有一个Session,如果Session占用的内存空间太大,会影响服务器的性能。


We should now look at an example of how to embed an OGNL expression in the location parameter value in order to pass some data forward to the second action. Note that this method of embedding OGNL to create dynamic parameter values can be used in any of the result types that support the parse parameter


The following example shows how we could pull a value from the ValueStack, at runtime, and pass that value as a querystring parameter to the URL: 

<action name="SendUserToSearchEngineAction" class="com.rk.struts.action.myActionClass">
 <result type=‘redirect‘ >
 http://www.google.com/?myParam=${defaultUsername}
 </result>
</action>


First of all, take careful notice of the fine point of the dollar sign($). We’ve been using the percent sign(%) throughout the book for our OGNL escape sequence, but in the context of our struts.xml, and other XML documents used for the declarative architecture, we must use a $ instead. Other than this inconsistency, the OGNL access still works the same. The OGNL looks up the property on the ValueStack. 


Provided that the imaginary myActionClass did something to make the defaultUsername property appear on the ValueStack, such as exposing it as a JavaBeans property, this result would render into a redirect response that’d point the browser to the following URL (assuming that the defaultUsername property, on the ValueStack, held the value of Mary): http://www.google.com/?myParam=Mary 



Note that you can use OGNL to build dynamic parameter values by pulling values from the ValueStack. You can do this to all the parameters that a result takes, as long as the result type itself supports the parsing of OGNL. 






2、redirect和redirection之间的区别


The redirectAction result does the same thing as the plain redirect result, with one important difference. This version of redirect(redirectAction) can understand the logical names of the Struts 2 actions as defined in your declarative architecture. This means that you don‘t have to embed real URLs in your result declarations. Instead you can feed the redirectAction names and namespaces from your action and package declarations. This makes your declarations more robust in the face of changes to URL patterns. 


As an example of such a URL pattern change, let’s say you wanted to change the action extension from .action to .go. If you’d used the plain redirect result extensively to target Struts 2 actions, then you’d have a lot of hard-coded URLs to adjust. 


使用redirect

<action name="Login" class="manning.chapterSeven.Login">
   <result type="redirect">
      /chapterSeven/secure/AdminPortfolio.action
   </result>
   <result name="input">/chapterSeven/Login.jsp</result>
</action>


使用redirectAction

<action name="Login" class="manning.chapterEight.Login">
   <result type="redirectAction">
      <param name="actionName">AdminPortfolio</param>
      <param name="namespace">/chapterEight/secure</param>
   </result>
   <result name="input">/chapterEight/Login.jsp</result>
</action>


Functionally, this is no different than the previous version. They both create a redirect response  that  points  to  the  following  URL:  

http://localhost:8080/manningSampleApp/chapterEight/secure/AdminPortfolio.action


The difference is that the redirectAction result would stand up to a variety of changes  in  the  URL  pattern  handling,  such  as  the  action  extension  we  mentioned earlier.



There is one other goodie supported by the redirectAction result. We can easily configure request parameters to be passed on to the target action. With the plain redirect,  we  had  to  write  the  querystring  parameter  out  by  hand.  Now  we  can  use  the param tag. In this case, we give the parameter whatever name and value we like. These arbitrary name-value pairs are appended as querystring parameters to the generated URL. 

<action name="Login" class="manning.chapterEight.Login">  
   <result type="redirectAction">
      <param name="actionName">AdminPortfolio</param>        
      <param name="namespace">/chapterEight/secure</param>
      <param name="param1">hardCodedValue</param>
      <param name="param2">${testProperty}</param>
   </result>
   <result name="input">/chapterEight/Login.jsp</result>
</action>


Here’s the new URL to which our result will redirect the browser, complete with our querystring  parameters:

 http://localhost:8080/manningSampleApp/chapterEight/secure/AdminPortfolio.action?param1=hardCodedValue&param2=777


As  you  can  see,  our  Login  action  must  have  done  something  to  make  the testProperty property exist on the ValueStack and hold the value of 777. 


This should be adequate to demonstrate how one goes about passing dynamic values into result parameter values with embedded OGNL. Again, any result that supports a parse parameter supports these kinds of techniques.


这些英文部分的内容来自:Struts2 in Action


以上是关于Struts2系列:(22) result 和OGNL的主要内容,如果未能解决你的问题,请参考以下文章

Struts2系列——struts2的result

Struts2-学习笔记系列(15)-ajax支持和JSON

Struts2系列:Action的访问路径和缺省后缀

Struts2-学习笔记系列-返回视图

Struts2框架---result结果集

Struts2中result的返回类型