STRUTS2 series: Result and OGNL

Source: Internet
Author: User

Simply describe the effect of OGNL and result:

(1) The value can be obtained from Actioncontext or valuestack via OGNL.

(2) for result, in Struts.xml, you can configure different processing results for the action.


This article focuses on 22 questions: (1) in the Struts.xml file, use OGNL to provide the dynamic parameter for result, (2) The difference between redirect and Redirectaction.


1. Use OGNL to provide dynamic parameter for result

In Struts.xml, the common result type has three types: dispatcher, redirect, and Redirectaction.

The main discussion here is the use of OGNL in redirect and Redirectaction types.


The reason is this: dispatcher is essentially using RequestDispatcher, and RequestDispatcher is a servlet api,requestdispatcher has a forward () method, Its function is to transfer the first servlet control to the second servlet, where it is important to note that the first servlet and the second servlet have one thing in common, which is that they handle the request and response as the same object, So the data they process (or receive parameters) is the same or can be shared, and in Struts2 's action code class, it has access to data such as request, response, Actioncontext, Valuestack, etc. And the JSP after dispatcher (essentially servlet) can be shared, in other words, after dispatcher JSP can be ognl in the page to get actioncontext and Valuestack values, Therefore, you do not need to provide the dynamic parameter for the result type of the dispatcher type.


However, we can provide dynamic parameter for dispatcher type of result, but it is not necessary.

and redirect and redirectaction are different objects before and after the request and response are processed, so in this case, if the first action wants to pass parameters to the address of the subsequent redirect, it needs to be passed through the querystring. , of course, can also be passed through the session, but it is not recommended to pass data with the session, because each user has a session, if the session occupies too large memory space, will affect the performance of the server.


We should now look at a example of how to embed a OGNL expression in the location parameter value in order to pass s ome data forward to the second action. Note that this method of embedding OGNL to create dynamic parameter values can is used in all of the result types that support for the parse parameter.


The following example shows how we could pulling a value from the Valuestack in runtime, and pass that value as a Querystrin G 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 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 is on the valuestack.


Provided that the imaginary myactionclass do something to make the DefaultUserName property appear on the Valuestack, suc h as exposing it as a JavaBeans property, this result would render into a redirect response that ' d point the browser to th E following URL (assuming 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 valuesfrom the Valuestack. The parameters that a result takes, as long as the result type itself supports the parsing of OGNL.






2. The difference between redirect and 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 D Eclarative architecture. This means, you don ' t has to embed real URLs in your result declarations. Instead you can feeds the redirectaction names and namespaces from your actionand package declarations. This makes your declarations more robust in the face of changes to URL patterns.


As an example of such a URL for pattern change, let's say the action extension from. Action to. Go. If you ' d used the plain redirect result extensively to target Struts 2 actions and then you ' d had a lot of hard-coded URLs t o Adjust.


Using redirect

<action name= "Login" class= "Manning.chapterSeven.Login" > <result type= "redirect" >/chapterseven/secure/ Adminportfolio.action </result> <result name= "Input" >/chapterseven/login.jsp</result></ Action>


Using 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, the redirectaction result would stand up to a variety of changes in the URL pattern Handli Ng, such as the action extension we mentioned earlier.



There is 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 is appended as QueryString parameters to the generated url. 

<action name= "Login" class= "Manning.chapterEight.Login" > <result type= "redirectaction" > <param nam E= "ActionName" >AdminPortfolio</param> <param name= "namespace" >/CHAPTEREIGHT/SECURE</PARAM&G      T <param name= "param1" >hardCodedValue</param> <param name= "param2" >${testProperty}</param> & lt;/result> <result name= "Input" >/chapterEight/Login.jsp</result></action>


Here's the new URL to which our result would redirect the browser, complete and our QueryString parameters:

http://localhost:8080/manningsampleapp/chaptereight/secure/adminportfolio.action?param1=hardcodedvalue& param2=777


As can see, our Login action must has done something to make the TestProperty property exist on the Valu Estack and hold the value of 777.


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


The contents of these English parts are from: Struts2 in Action


STRUTS2 series: Result and OGNL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.