Result of the STRUTS2 series--struts2

Source: Internet
Author: User

A string is always returned after execution of the specified method of the action, struts2 to find a matching name based on the returned string to the result of the action's configuration, and performs the next action according to the configuration.


Five standard return values defined in the Actionsupport base class


String SUCCESS = "SUCCESS";

String none = "None";

String error = "Error";

String input = "INPUT"; String login = "Login";
Of course we can define the name of the return by ourselves.


The result element has two uses, first it provides a logical name. An action can simply return success or input without ignoring the specifics. Second, the result element provides a type attribute that can be used to not only return a JSP page, but also to achieve more interesting functionality.

Each package can have its own default result type, which uses the default when a result does not specify a type. Under normal circumstances, the default result type is dispatcher. If a package inherits from another package, the package can implement its own default result or inherit the default result of the parent package. Manually specifying a default result is as follows


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


Also, the Name property of a result has a default value of success. In the most common case, a result is as follows

<result>
/thankyou.jsp
</result>

An action can have multiple different result

<action name= "Hello" >
<result>/hello/Result.jsp</result>
<result name= "Error" >/hello/Error.jsp</result>
<result name= "Input" >/hello/Input.jsp</result>
</action>
Global result

Sometimes, some result can be used for all action services, such as the result of the error page, the result of landing page

We can define some global result for all actoin sharing of the same package. Note that first struts2 will search for local result, and if not found, it will go to global result to find a matching result

<global-results>
<result name= "Error" >/Error.jsp</result>
<result name= "Invalid.token" >/Error.jsp</result>
<result name= "Login" type= "redirectaction" >Logon!input</result>
</global-results>

Dynamic parameter configuration for result

There are times when we need to move from one action to another, but the arguments are run to know, and can be implemented in a way. Let's use an example to illustrate

<struts>
....
<package name= "Somepackage" namespace= "/mynamespace" extends= "Struts-default" >
<action name= "myaction" class= "Com.project.MyAction" >
<result name= "Success" type= "Redirectaction" >otherAction?id=${id}</result>
<result name= "Back" type= "redirect" >${redirectURL}</result>
</action>

<action name= "otheraction" class= "Com.project.MyOtherAction" >
...
</action>
</package>
....
</struts>


You must have the Id,redirecturl property and their get method in the action

public class Myaction extends Actionsupport {
private int id;
Private String RedirectURL;
...
Public String execute () {
...
if (somecondition) {
This.redirecturl = "/the/target/page.action";
return "Back";
}

This.id = 123;
return SUCCESS;
}

public int getId () {return this.id;}
public void setId (int id) {this.id = ID;}
Public String Getredirecturl () {return this.redirecturl;}
public void Setredirecturl (String redirecturl) {this.redirecturl=redirecturl;}
...
}

If you return to success, you will be transferred to/<app-prefix>/mynamespace/otheraction.action?id=123.


When a result has more than one argument, it can be specified by the Param child property, which is followed by an example

Types of result


Result has many different types, used to implement different functions, struts2 default types has the following several


Dispatcher go to a view page, usually a JSP page. This is the default type value.

<result>
/thankyou.jsp
</result>

Stream sends raw data bytes to the browser, usually for downloading files


ContentType the Mime-type (default text/plain) of the stream sent to the browser.

contentlength-the length of the stream (for easy browser display of download progress).

contentdispostion-setting the value of the response header contentdispostion (default inline)

I'm not sure what that means, Google has a little bit of a good explanation.

InputName the property name of the input stream provided by the action (default InputStream).

BufferSize the cache size (default 1024 bytes) written from the input stream to the output stream.

<result name= "Success" type= "Stream" >
<param name= "ContentType" >image/jpeg</param>
<param name= "InputName" >imageStream</param>
<param name= "BufferSize" >1024</param>
</result>

PlainText is typically used to display the original content of a JSP or HTML page

<action name= "Displayjsprawcontent" >
<result type= "PlainText" >/myJspFile.jsp</result>
</action>

Redirectaction directing to another action doesn't feel much use, for example, for reference.

<package name= "public" extends= "Struts-default";
    <action name= "login" class= "..."
        <!--Redirect to another namespace-->
         <result type= "redirect-action";
             <param name= "ActionName" >DASHBOARD</PARAM>
             <param name= "namespace" >/SECURE</PARAM>
         </result>
    </action>
</package>

<package name= "secure" extends= "Struts-default" namespace= "/secure" >
<--Redirect to a action in the same namespace-
<action name= "Dashboard" class= "..." >
<result>dashboard.jsp</result>
<result name= "error" type= "Redirect-action" >error</result>
</action>

<action name= "error" class= "..." >
<result>error.jsp</result>
</action>
</package>

<package name= "Passingrequestparameters" extends= "Struts-default" namespace= "/passingrequestparameters" >
<--Pass parameters (reporttype, width and height)--
<!--
The Redirect-action URL generated'll be:
/genreport/generatereport.action?reporttype=piewidth=100height=100
-
<action name= "Gatherreportinfo" class= "..." >
<result name= "Showreportresult" type= "Redirect-action" >
<param name= "ActionName" >generateReport</param>
<param name= "namespace" >/genReport</param>
<param name= "Reporttype" >pie</param>
<param name= "width" >100</param>
<param name= "Height" >100</param>
</result>
</action>
</package>

It feels like the type is a little more useful, and the other few don't write. I'll write it later when it's useful.

Result of the STRUTS2 series--struts2

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.