A processing submitted to the server can usually be divided into two stages. The first stage queries the server status (queries or updates the database), and the second stage selects a suitable result page and returns it to the user (the content of the Result to be discussed here).
Struts2 provides support for different kinds of return results, common ones include JSP, FreeMarker, Velocity, etc.
The different types of return results supported by Struts2 are:
| name | illustrate |
|---|---|
| Chain Results | Used to handle Action chains |
| Dispatcher Results | Used to turn to pages, usually handle JSP |
| FreeMarker Results | Handle FreeMarker templates |
| HttpHeader Results | Used to control special Http behavior |
| Redirect Result | Redirect to a URL |
| Redirect Action Results | Redirect to an Action |
| Stream Results | Send InputSream object to the browser, usually used to process file downloads |
| Velocity Results | Handle Velocity templates |
| XLS Results | Process XML/XLST templates |
| PlainText Result | Display original file content, such as file source code |
| S2PLUGINS:Tiles Result | Use in combination with Tile |
In addition, the third-party Result type also includes the JasperReports Plugin, which is specially used to process report output of the JasperReport type.
There are already definitions for all types of Result 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.structs2.dispatcher.HttpHeaderResult"/><result-type name="redirect"class="org.apache.structs2.dispatcher.ServletRedirectResult"/><result-type name="redirectAction"class="org.apache.structs2.dispatcher.ServletActionRedirectResult"/><result-type name="stream"class="org.apache.structs2.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" /><!-- 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.structs2.dispatcher.ServletActionRedirectResult"/><result-type name="plaintext"class="org.apache.struts2.dispatcher.PlainTextResult" /></result-types>
From the above code, we can see that when the Result type is not specified, it is used.
Define a Result value,
<result name="success" type="dispatcher"><param name="location">/ThankYou.jsp</param></result>
Since the default value of type is dispatcher, there is no need to define it here. In addition, the default value of name is success, so there is no need to define it here.
The above code can be abbreviated as:
<result><param name="location">/ThankYou.jsp</param></result>
In addition, the location parameters can also be directly uninstalled from the result tag, so the simplest way to write 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 meaning of the above code is that Action with the name Hello has three returns, and they are all dispatcher types (default type). The names of these three return values are success (default value), error, and input. The corresponding page paths are /hello/Result.jsp, /hello/Error.jsp, and /hello/Input.jsp.
Sometimes we need a global Result defined. At this time, we can define a global Result inside 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>
Dynamically return results
Sometimes, we only know which result to return when the Action executes the Complete Bi. At this time, we can define a property inside the Action, which is used to store the Result value after the Action executes the Complete Bi, for example:
private String nextAction;public String getNextAction() { return nextAction;} In the strutx.xml configuration file, we can use ${nextAction} to reference the properties in the Action, and dynamically return the result through the content represented by ${nextAction}, for example:
<action name="fragment"><result name="next" type="redirect-action">${nextAction}</result></action> When the execute method of the above Action returns next, it is also necessary to determine which Action is located based on the properties of nextAction.
If you want to forward to another action, you can set type=chain and the result will not be added to shtml.
The above is all the detailed explanation of the Struts2 Result parameters. I hope you can give you a reference and I hope you can support Wulin.com more.