Two Methods for converting struts2 from one action to another.

Source: Internet
Author: User

InStruts2Medium,ActionAfter the user request is processed, A String object is returned, which is a logical view name.Struts 2By configuring the ing between the logical view name and physical view, once the system receivesActionThe system displays the corresponding physical view to the viewer.

Struts 2The result type determinesActionAfter processing, the next step will call the view resource to present the processing result. The default value isDispatcher, This result type is specified to useJSPAs a view resource. However, we have the following requirement:ActionGo to anotherAction.

 FromActionJump directly to anotherActionMedium,StrutsTwo result types are available:Chain,Redirect.

FromServlet Parameters cannot be retained after redirection, that is, redirection, Request The object will be invalid. Similarly Redirect Just like Servlet And it cannot be retained. Request Parameter of the object. However Chain Yes. So the difference between the two is that Chain Retainable Request Object, and Redirect No.

 

Struts. xml configuration

 1   <  Package  Name  = "Mystruts1"  Extends  = "Struts-Default"  Namespace  = "/Mystruts1" >  2           <! --  First Action  -->  3           <  Action  Name  = "Test _*"  Class  = "Com. Action. testaction"  Method  = "{1 }"  >  4               < Result  Name  = "Text_chain"  Type  = "Chain"  > Result_resultchain </  Result  >  5               <  Result  Name  = "Text_redirect"  Type  = "Redirect" > Result_resultredirect </  Result  >  6           </  Action  >  7           8           <! --  Second Action  -->  9           <  Action  Name = "Result _*"  Class  = "Com. Action. resultaction"  Method  = "{1 }"  > </  Action  >  10       </  Package  > 

Action1:

 1   Public   Class Testaction {  2       Public  String testchain (){  3 Httpservletrequest request = (Httpservletrequest) actioncontext. getcontext (). Get (servletactioncontext. http_request );  4 Request. setattribute ("Param", "I am a chain result type ..." );  5           Return "Text_chain" ;  6   } 7       8       Public  String testredirect (){  9 Httpservletrequest request = (Httpservletrequest) actioncontext. getcontext (). Get (servletactioncontext. http_request );  10 Request. setattribute ("Param", "I am a redirect result type ..." );  11           Return "Text_redirect" ;  12   } 13 }

Action2:

 1   Public   Class  Resultaction {  2   3       Public  String resultchain (){  4 Httpservletrequest request = (Httpservletrequest) actioncontext. getcontext (). Get (servletactioncontext. http_request );  5 System. Out. println (request. getattribute ("Param" ));  6           Return   Null  ;  7   }  8       9       10       Public  String resultredirect (){  11 Httpservletrequest request = (Httpservletrequest) actioncontext. getcontext (). Get (servletactioncontext. http_request ); 12 System. Out. println (request. getattribute ("Param" ));  13           Return   Null  ;  14   }  15 }

 After the project is deployed, enter:Http: // localhost: 8080/struts2_01/mystruts1/result_resultredirect.action. The result is as follows:

 Returns null. Therefore, redirect does not obtain the parameters saved in the request object.

 

When you enterHttp: // localhost: 8080/struts2_01/mystruts1/result_testchain.action, The following result is obtained:

 The correct result is obtained. All chains can obtain the parameters saved by the request object.

 Therefore:

 1. When you need to save the attribute information of the previous action, save the request object of Action1:

 
1 <ResultType= "Chain"Name= "A2">Action2</Result> 

 2. Do not save the parameter of the previous action. You can use this method:

1<ResultType= "Redirectaction">Action2</Result> 

 

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.