Transferred from:
Http://zhangxiang390.javaeye.com/blog/231858
Http://hi.baidu.com/matrix286/blog/item/f659f5919c92e484a977a491.html
Http://blog.csdn.net/struts2/archive/2007/08/08/1731386.aspx
The processing of a request submitted to the server can be divided into two phases. The first stage queries the server status (queries or updates the database ), in the second stage, select an appropriate result page and return it to the user (the content of the result here ).
Struts2 supports different types of returned results, such as JSP, freemarker, and velocity.
Struts2 supports different types of returned results:
Description
Chain result
Used to process the action chain
Dispatcher result
It is used to redirect to a page.
Freemarker result
Process freemarker templates
Httpheader result
Used to control special HTTP Behaviors
Redirect result
Redirect to a URL
Redirect action result
Redirect to an action
Stream result
Sends an inputsream object to a browser, which is usually used to process file downloads.
Velocity result
Process the velocity Template
XLS result
Process XML/XLST templates
Plaintext result
Displays the content of the original file, such as the source code of the file.
S2plugins: tiles result
Combined with tile
In addition, the third-party result type also includes jasperreports plugin, which is specially used to process the report output of the jasperreport type.
The definition of all types of results already exists in the struts-default.xml file:
<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"/>
<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. javastresult"/>
<Result-type name = "plaintext"
Class = "org. Apache. struts2.dispatcher. plaintextresult"/>
<! -- Deprecated name form scheduled for removal in struts 2.1.0.
The camelcase versions are preferred. See ww-1707 -->
<Result-type name = "Redirect-action"
Class = "org. Apache. struts2.dispatcher. servletactionredirectresult"/>
<Result-type name = "plaintext"
Class = "org. Apache. struts2.dispatcher. plaintextresult"/>
</Result-types>
The above Code shows that the dispatcher type is used when the result type is not specified.
Define a result value,
<Result name = "success" type = "dispatcher">
<Param name = "location">/thankyou. jsp </param>
</Result>
Because the default value of type is dispatcher, it is not required here. In addition, the default value of name is success, so it is not required here.
The preceding code can be abbreviated:
<Result>
<Param name = "location">/thankyou. jsp </param>
</Result>
In addition, the location parameter can also be directly uninstalled inside the result tag. Therefore, the simplest method of the above Code is:
<Result>/thankyou. jsp </result>
We can also define multiple different results.
<Action name = "hello">
<Result>/Hello/result. jsp </result>
<Result name = "error">/Hello/error. jsp </result>
<Result name = "input">/Hello/input. jsp </result>
</Action>
The preceding Code indicates that the action named Hello has three returned results, all of which are of the dispatcher type (default type). The return values are success (default value ), error, input, and the corresponding page paths are/Hello/result. JSP,/Hello/error. JSP,/Hello/input. JSP.
Sometimes we need a global result. In this case, we can define the global result in the package, for example:
<Global-Results>
<Result name = "error">/error. jsp </result>
<Result name = "invalid. Token">/error. jsp </result>
<Result name = "login" type = "Redirect-action"> logon! Input </result>
</Global-Results>
Dynamic return results
Sometimes, we only know the result to be returned when the action is executed. In this case, we can define an attribute inside the action, this attribute is used to store the result value after the action is executed. For example:
Private string nextaction;
Public String getnextaction (){
Return nextaction;
}
In the strutx. xml configuration file, we can use $ {nextaction} to reference the attributes in the action and dynamically return the results using the content represented by $ {nextaction}. For example:
<Action name = "fragment" class = "fragmentaction">
<Result name = "Next" type = "Redirect-action" >$ {nextaction} </result>
</Action>
When the execute method of the preceding action returns next, you need to determine which action to locate Based on the nextaction attribute.