"Struts2" Result and Resulttype

Source: Internet
Author: User
Tags xslt

In a nutshell, result is a string that is returned after the action executes, indicating where the next page is after the action is completed. Result is just a string, just to indicate the next page, so how do you get to the next page? How can the next page show the results correctly? This should lead to a new concept--resulttype, the so-called Resulttype, refers to the specific execution of result class, which determines which view technology to use, the execution results are presented to the user.

Many times, we do not distinguish between result and resulttype, but are generally called result. As a result, result can be used as a technique in addition to the string described above, and result as a technique for implementing view views in the MVC model, that is, Resulttype.

In Struts2, you can use a variety of view technologies, such as JSP, Freemarker, Velocity, jfreechart, and so on, while Struts2 also supports user-defined resulttype to create your own view technology.

1) Pre-defined Resulttype

In Struts2, a lot of resulttype are predefined, which is a technique that defines many of the results of the presentation. Struts2 put the built-in <result-type> in the Struts-default package. The Struts-default package is the parent package of the package that we configured, which is defined in the file Struts-default.xml in the root directory of the Struts-2.3.16.3.jar package. In this package, you can find the relevant <result-type> definition,<result-types> element is the direct child element of the <package> element. STRUTS2 is predefined as follows:

<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-t ype name= "redirect" class= "Org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name= "Redi Rectaction "class=" Org.apache.struts2.dispatcher.ServletActionRedirectResult "/> <result-type name=" stream "class=" Org.apache.struts2.dispatcher.StreamResult "/> <result-type name=" Velocity "class=" org.apache.str Uts2.dispatcher.VelocityResult "/> <result-type name=" xslt "class=" Org.apache.struts2.views.xslt.XSLTResul T "/> <result-Type name= "plaintext" class= "Org.apache.struts2.dispatcher.PlainTextResult"/> <result-type name= "PostBac K "class=" Org.apache.struts2.dispatcher.PostbackResult "/> </result-types>

Each of the above <result-type> elements is an encapsulation of a view technique or a jump method. The Name property is how this view technique or jump is referenced in the <result> element, corresponding to the value of the Type property of the <result> element.

A friend might say that we did not configure the type attribute when we configured the <result> element. Yes, you did not configure, the reason is that Struts2 set the default type, if you do not configure, the default is "dispatcher". This "dispatcher" technology is equivalent to the "requestdispatcher" technology in the servlet, that is, a page jump technology. The class attribute is the concrete implementation class of this kind of view technique or jump way, these implementation classes are already STRUTS2 implementation good, we just need to reference on it.

2) the configuration of result

    • Configure the Name property

The Name property is used to correspond to the string returned by the Execute method of the action, which indicates the next page to which the action will jump, so the value of the Name property can be any string. For example, there are the following Execute methods:

Public String Execute () throws Exception {          return "towelcome";  }  

So, the "Towelcome" returned here, in the Struts.xml is like the following configuration to correspond:

<action name= "helloworldaction" class= "cn.javass.action.action.HelloWorldAction" >              <result name= " Towelcome ">/s2impl/welcome.jsp</result>  </action>  

If not set, the default value is "Success", which corresponds to the constant of "success" in action, so that the Execute method should return success, as follows:

Public String Execute () throws Exception {          return this. SUCCESS;  }  

At this point in the Struts.xml is like the following configuration to correspond:

<action name= "helloworldaction" class= "cn.javass.action.action.HelloWorldAction" >              <result>/ S2impl/welcome.jsp</result>              <!--or the following configuration             <result name= "Success" >/s2impl/welcome.jsp</ Result>            --></action>
    • Configuring the Type Property

The type attribute of the <result> element can also be any string, but must be the name property of a certain <result-type> element. In the absence of a custom resulttype, the value of the Type property is the value of the <result-type> Name property defined in Struts-default.xml. Like what:

<action name= "helloworldaction" class= "cn.javass.action.action.HelloWorldAction" >              <result name= " Towelcome "type=" Dispatcher ">/s2impl/welcome.jsp</result>  </action>  

The "dispatcher" here is the default <result-type> name attribute value defined in the Struts-default.xml. Since it is the default, it can be used without configuration, that is to say, the above configuration is equivalent to the following configuration, the example is as follows:

<action name= "helloworldaction" class= "cn.javass.action.action.HelloWorldAction" >              <result name= " Towelcome ">/s2impl/welcome.jsp</result>  </action>  

3) Global Result

    • Configuration method

The global result itself does not have any peculiarities, as is the configuration of the Name property and the Type property, including how the JSP is specified in the same location as the normal result, except that its <result> element is not a child element of the <action> element. Instead, as a child of the <global-results> element, and the <global-results> element is a child of the <package> element, the example is as follows:

<package name= "HelloWorld"  extends= "Struts-default" >      <global-results>          <result name= " Tologin ">/login.jsp</result>      </global-results>        <action ......> ...      </action>  </package>  
    • Search for the order of result

After you have the global result, you need to discuss the order of the result based on the return value of the Execute method after the action is run.

(1) First, find your own <action> element within the <result> element if there is a match, if there is to execute this result, if not, the next step.

(2) Second, look for the global result of their <action> package, see if there is a match, if there is to execute this result, if not, next.

(3) Again, recursively look for the parent package of its own package, the global result in the grandfather package whether there is a match, if there is to execute this result, if not, the next step.

(4) Finally, if the above three cases do not match the result, then throw exception.

Note: If result with the same name appears, the order above is also the order of precedence between the result. That is, if the string returned by the Execute method of the action has a matching configuration in both local result and global result, then local result will prevail.

Reference: http://www.iteye.com/topic/1124526

"Struts2" Result and Resulttype

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.