STRUTS2 Series: (6) Result-type and global result

Source: Internet
Author: User
Tags xslt

The package, action, and result in (2) to Struts.xml are briefly described.

The action is described in more detail in (3).

In this section, result is described in some detail, consisting mainly of two parts: Result-type and global result.



1, Result-type

# Code Angle #在Action类中, each action method returns a value of type String, and Struts will determine what result to respond to based on this value.

# Configure Angle #在struts. In an XML configuration file, each action tag can contain multiple result elements, each of which corresponds to a return value of the action method.


property of the result tag

The Name:result name, the default value of success, matches the return value of the Action method, or an error may occur.

Type: result types. The default value is dispatcher


Our problem is focused on this point, what are the values of type?


The answer to this question requires the help of the Struts-default.xml file, which is located under Struts2-core-2.3.29.jar.

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/83/0C/wKioL1dpph7ispjUAAEU8eURYRc072.png "title=" struts _default_xml. PNG "alt=" Wkiol1dpph7ispjuaaeu8euryrc072.png "/>


The package named Struts-default was found in Struts-default.xml, which defines many result-type.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/0D/wKiom1dppsrT1iGrAAEk8OeIgp0932.png "title=" Result-type. PNG "alt=" Wkiom1dppsrt1igraaek8oeigp0932.png "/>

        <result-types>             <result-type name= "Chain"  class= " Com.opensymphony.xwork2.ActionChainResult "/>             <result-type name= "Dispatcher"  class= " Org.apache.struts2.dispatcher.ServletDispatcherResult " default=" true "/>             <result-type name= "Freemarker"  class= " Org.apache.struts2.views.freemarker.FreemarkerResult "/>             <result-type name= "Httpheader"  class= " Org.apache.struts2.dispatcher.HttpHeaderResult "/>             <result-type name= "redirect"  class= " Org.apache.struts2.dispatcher.ServletRedirectResult "/>     &Nbsp;      <result-type name= "Redirectaction"  class= " Org.apache.struts2.dispatcher.ServletActionRedirectResult "/>             <result-type name= "Stream"  class= " Org.apache.struts2.dispatcher.StreamResult "/>             <result-type name= "Velocity"  class= "org.apache.struts2.dispatcher.VelocityResult"/ >            <result-type name= "XSLT"  class= "Org.apache.struts2.views.xslt.XSLTResult"/>             <result-type name= "PlainText"  class= " Org.apache.struts2.dispatcher.PlainTextResult " />             <result-type name= "Postback"  class= " Org.apache.struts2.dispatcher.PostbackReSult " />        </result-types> 

Here, we only care about three of them:Dispatcher, redirect, Redirectaction.

<result-types> <result-type name= "Dispatcher" class= "org.apache.struts2.dispatcher.ServletDispatcherRe Sult "default=" true "/> <result-type name=" redirect "class=" Org.apache.struts2.dispatcher.ServletRedirectR Esult "/> <result-type name=" redirectaction "class=" org.apache.struts2.dispatcher.ServletActionRedirectRes Ult "/> </result-types>


1.1, Dispatcher

<result-type name= "Dispatcher" class= "Org.apache.struts2.dispatcher.ServletDispatcherResult" default= "true"/ >


Servletdispatcherresult

Full Name: Org.apache.struts2.dispatcher.ServletDispatcherResult

(1) Include a view (includes a) or forward to a view (forwards to a).

Includes or forwards to a view (usually a JSP).

(2) Implementation mechanism: Struts will use Requestdespatcher.

Behind The scenes Struts would use a requestdispatcher, where the target servlet/jsp receives the same request/response obj ECTs as the original servlet/jsp. Therefore, you can pass data between them using Request.setattribute ()-The Struts action is available.

(3) The accepted parameters are 2: Location and parse

This result type takes the following parameters:

Location (default)-the location to go to after execution (ex. jsp).

Parse -True by default. If set to False, the location Param won't is parsed for Ognl expressions.



1.2, redirect

<result-type name= "redirect" class= "Org.apache.struts2.dispatcher.ServletRedirectResult"/>


Servletredirectresult

Full Name: Org.apache.struts2.dispatcher.ServletRedirectResult

(1) Actual mechanism: Call Response.sendredirect method

Calls the Sendredirect method to the location specified. The response is told to redirect, the browser to the specified location (a new request from the client). The consequence of doing this means the action (action instance, action errors, field errors, etc) that is just exec Uted is lost and no longer available. This is because actions was built on a single-thread model.

(2) If you want to pass parameters when using redirect, you can only use session and Web parameters (Url?name=value).

The only-to-pass data is through the session or with Web parameters (Url?name=value) which can OGNL expressions.

(3) The accepted parameters are 3: location, parse and anchor

This result type takes the following parameters:

Location (default)-the location to go to after execution.

Parse -True by default. If set to False, the location Param won't is parsed for Ognl expressions.

anchor -Optional. Also known as "fragment" or colloquially as "hash". You can specify an anchor for a result.



1.3, Redirectaction

<result-type name= "redirectaction" class= "Org.apache.struts2.dispatcher.ServletActionRedirectResult"/>


Servletactionredirectresult

Full Name: Org.apache.struts2.dispatcher.ServletActionRedirectResult

(1) Implementation mechanism: Actionmapperfactory Factory provides actionmapper,actionmapper to guide the browser to a URL.

This result uses the Actionmapper provided by the Actionmapperfactory to redirect the browser-a URL that invokes the SP Ecified action and (optional) namespace.

(2) Servletactionredirectresult is better than Servletredirectresult. It is recommended to use Servletactionredirectresult.

This is better than the servletredirectresult because it does don't require you to encode the URL patterns processed by the Actionmapper in to your struts.xml configuration files. This means your can change your URL patterns at any point and your application would still work. It is strongly recommended so if you were redirecting to another action, you use this result rather than the standard red Irect result.

(3) The accepted parameters are 5: ActionName, Namespace, Suppressemptyparameters, parse and anchor.

ActionName (default)-The name of the action that is redirected to.

namespace -used to determine which namespace the action was in that we ' re redirecting to. If namespace is null, the default would be being the current namespace.

suppressemptyparameters -Optional Boolean (defaults to false) so can prevent parameters with no values from IS ing included in the redirect URL.

Parse -Boolean, True by default. If set to False, the actionname param is not being parsed for OGNL expressions.

anchor -Optional. Also known as "fragment" or colloquially as "hash". You can specify an anchor for a result.

dispatcher------type Servletdispatcherresult The result type has a  location  parameter ,  It is a default parameter forwarding control  redirect-----  Type Servletredirectresult response Redirect to another resource (including external resources),  not forwarding redirect   parameter:location:  redirect Destination param:  whether the value of the  location  parameter is treated as a  OGNL  expression to interpret the .  default value of  trueredirectaction response Redirect to another  actionredirectaction type servletactionredirectresult,       as Servletdispatcherresult subclass, redirectaction result type and redirect result type background work, That is, the HttpServletResponse Sendredirect method is used to redirect the request to the specified URL. Redirectaction is primarily used to redirect to action. If you need to redirect to another action, we recommend using Redirectaction. 
<package name= "Default" extends= "Struts-default" > <action name= "test1" class= "Com.rupeng.Action1" > < Result type= "Redirectaction" > <param name= "actionname" >test2.action</param> <param name= "Namespace" >/test2NS</param> </result> </action></package><package name= "secure" ext  ends= "Struts-default" namespace= "/test2ns" > <action name= "test2" class= "Com.rupeng.Action2" > <result Name= "Success" >/success.jsp</result> </action></package>





2. Global result

Global result

Concept: Result that can be used together in multiple actions.

<package ....> <global-results> <result name= "Globalsuccess" >/globalSuccess.jsp</result> </global-results></package>


Local and global occurrence of the same result, local function ("Strong dragon does not pressure local bully")




STRUTS2 Series: (6) Result-type and global 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.