Chapter 8 result types: the process of submitting a result to the server can be divided into two stages. 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:
Name |
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. extends tresult "/> <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 to be defined here. In addition, the default value of name is success, which is not required here. The preceding code can be abbreviated to <result> <Param name = "location">/thankyou. JSP </param> </result> In addition, the location parameter can also be directly detached from the result tag. Therefore, the simplest syntax 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 names of the three return values are success (Values), error, input, and 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 resultsSometimes, 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;} is in strutx. in the xml configuration file, we can use $ {nextaction} to reference the attribute in the action, and dynamically return the result through 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 also need to determine which action to locate Based on the nextaction attribute.