Struts2 Learning -- Result

Source: Internet
Author: User
Tags xslt

Directory

1. struts2 result. 1

2. result type... 1

3. Differences between chain, redirect, and redirectaction... 3

4. Result's understanding about param... 3

5. Global View... 4

6. Custom extension) result type... 4

1. struts2 result

A process submitted to the server can be divided into two phases: the first stage queries the service status query or updates the database), and the second stage selects an appropriate result page and returns it to the user.

Struts2 supports different types of returned results, such as JSP, FreeMarker, and Velocity.

Struts2 supports the same return type:

Chain is used to process the Action chain

Dispatcher is used to redirect to a page, which is usually used to process JSP by default)

Freemarker processes FreeMarket templates

Httpheader is used to control special Http behaviors.

Redirect redirects to a URL

RedirectAction redirects to an Action

Stream sends an InputStream object to the browser, which is usually used to process file downloads.

Velocity processing Velocity Template

Xslt processing XML/XSLT templates

Plaintext displays the content of the original file, such as the source code of the file.


2. result type

1) dispatcher: the server jumps to the page, which is usually used to process JSP. The default type is.

2) redirect: redirects to the page.

Public String redirect (){

Message = "values in message ";

Return "redirect ";

}

Struts. xml

<Package name = "chapter3" namespace = "/chapter3" extends = "struts-default">

<Action name = "redirectAction"

Class = "chapter3.action. Chapter3Action" method = "redirect">

<Result name = "redirect" type = "redirect">/redirect. jsp </result>

</Action>

</Package>

Note: The External transit cannot carry a value in the past, and the page cannot be protected

PASS Parameters:

<Action name = "redirectAction" class = "chapter3.action. Chapter3Action" method = "redirect">

<Result name = "redirect" type = "redirect">/redirect. jsp? Message =$ {message} </result>

</Action>

Page:

$ {Param. message} <br>

3) chain type

Server internal) Jump to other actions;

Action:

Public String action2 (){

Message = "value set in action2 ";

Return "action2 ";

}

Struts. xml

<Package name = "chapter32" namespace = "/chapter32" extends = "struts-default">

<Action name = "action2" class = "chapter3.action. Chapter3Action" method = "action2">

<Result name = "action2" type = "chain">

<Paramname = "actionName"> redirectAction </param>

<Param name = "namespace">/chapter3 </param>

</Result>

</Action>

</Package>

4) redirectAction: jump from external to Action;

Action:

Public String action3 (){

Message = "value set in action3 ";

Return "action3 ";

}

Struts. xml

<Action name = "action3" class = "chapter3.action. Chapter3Action" method = "action3">

<Result name = "action3" type = "redirectAction"> redirectAction </result>

</Action>

Cross-namespace external jump:

Public String action4 (){

Message = "I am the value set in action4 ";

Return "action4 ";

}

Struts. xml

<Action name = "action4" class = "chapter3.action. Chapter3Action" method = "action4">

<Result name = "action4" type = "redirectAction">

<Param name = "actionName"> redirectAction </param>

<Param name = "namespace">/chapter3 </param>

</Result>

</Action>


3. Differences between chain, redirect, and redirectaction

Chain Result:

This result calls another action to connect the interceptor stack and result.

ActionName (default)-name of the called action

Namespace-namespace of the called action. If the namespace is empty, this is the current namespace by default.

Method-another method used to specify the target action is called. If it is null, the default value is the excute method.

Redirect Action Result:

This Result uses the ActionMapper provided by ActionMapperFactory to relocate the browser URL to call the specified action and (optional) namespace. You can change the URL mode at any point without affecting your application. Therefore, we strongly recommend that you use this Result instead of the standard redirect result to relocate to an action.

ActionName (default)-Relocated action name

Namespace-action namespace. If it is null, it is the current namespace

Redirect Result:

Call the {@ link HttpServletResponse # sendRedirect (String) sendRedirect} method to go to the specified position. The HTTP response is notified that the browser will jump directly to the specified location (a new request from the client is generated ). The result will cause the newly executed action (including the action instance and error messages in the action) to be lost and no longer available. This is because action is built on a single-threaded model. The only way to pass data is through Session or a web parameter that can be an Ognl expression (url? Name = value ).

Location (default)-address to jump after the action is executed.

Parse-the default value is true. If it is set to false, the location parameter is not parsed as an Ognl expression.



Note:

1) When type = "redirectAction" or type = "redirect" is used to submit to an action and a parameter needs to be passed. There is a difference here:. when type = "redirectAction" is used, only the configuration name of the Action can be written in the result, and the suffix ". action "; B. when type = "redirect" is used, the result should be the action configuration name + suffix name

2) redirect: after an action is processed, it is redirected to a view resource such as a jsp page.) All Request Parameters and action processing results are lost.

3) redirect-action: after the action is processed, it is redirected to an action. All Request Parameters and action processing results are lost.

4) chain: after the action is processed, it is forwarded to an action. All request parameters are lost, and the action processing result is not lost.


4. Result's understanding about param

Param parameter settings:

Name: Optional. It specifies the parameter name.

Value: an optional attribute that specifies the value of a parameter.

It is generally used to return json, stream, and characters.

5. Global View
<Package name = "test3" namespace = "/test3" extends = "struts-default"> <! -- Global View --> <Global-results> <result name = "message">/WEB-INF/page/global. jsp </result> </global-results> <! -- Use the global view. If <action> does not contain the corresponding <result>, Struts2 uses the Global <result> --> <action name = "message" class = "cn. action. actionTest "method =" global "> </action> </package> <! -To use the global view in other packages, you must use the following package inheritance: --> <package name = "base" extends = "struts-default"> <! -- Global View --> <Global-results> <result name = "message">/WEB-INF/page/global. jsp </result> </global-results> </package> <! -- Use the global view extends = "base" --> <package name = "test4" namespace = "/test4" extends = "base"> <action name = "message" class =" cn. action. actionTest "method =" global "> </action> </package>
6. Custom extension) result type

Find information to know the http://hi.baidu.com/hfsdaccp/item/26e261873d025655840fabaa) custom implementation method:


Implementation Method: directly expand struts2 result set StrutsResultSupport

For example, the return type is String:

public class StringResultType extends StrutsResultSupport {    private static final long serialVersionUID = 1L;    private String contentTypeName;    private String stringName = "";    public StringResultType() {        super();    }    public StringResultType(String location) {        super(location);    }    protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {        HttpServletResponse response = (HttpServletResponse) invocation                .getInvocationContext().get(HTTP_RESPONSE);        // String contentType = (String)        // invocation.getStack().findValue(conditionalParse(contentTypeName,        // invocation));        String contentType = conditionalParse(contentTypeName, invocation);        if (contentType == null) {            contentType = "text/plain; charset=UTF-8";        }        response.setContentType(contentType);        PrintWriter out = response.getWriter();        // String result = conditionalParse(stringName, invocation);        String result = (String) invocation.getStack().findValue(stringName);        out.println(result);        out.flush();        out.close();    }    public String getContentTypeName() {        return contentTypeName;    }    public void setContentTypeName(String contentTypeName) {        this.contentTypeName = contentTypeName;    }    public String getStringName() {        return stringName;    }    public void setStringName(String stringName) {        this.stringName = stringName;    }}

Usage:

Action:

package test;import com.opensymphony.xwork2.ActionSupport;public class MyAction extends ActionSupport{    String  result="abc";    public String ajax() {        return "ajaxResponse";    }    public String getResult() {        return result;    }    public void setResult(String result) {        this.result = result;    }}

Struts. xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <package name="test" extends="struts-default">        <result-types>            <result-type name="string" class="test.StringResultType"></result-type>        </result-types>              <action name="myAction" class="test.MyAction" >            <result name="ajaxResponse" type="string">                <param name="stringName">result</param>            </result>        </action>    </package></struts>

It is nothing more than declaring the string result set and binding the variable name to the result that returns "ajaxResponse. Here, the String result variable of the returned Action is defined, that is, "abc ".


This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1286614

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.