Struts2 framework (5) --- result set, struts2 --- result

Source: Internet
Author: User

Struts2 framework (5) --- result set, struts2 --- result
Result set

The previous article focuses on the Struts2 framework (4) --- Action class access servlet This article focuses on the result set

The result element in Struts. xml indicates the result view after the action methods of the action class are executed.

(1) local and global results

It has two attributes:

Name: String, consistent with the value returned by the Action method. The default value is success.

Type: Specifies the alias of the result type of the processing result. The default value is dispatcher.

First, the processing results are divided into two types: local results and global results.

Partial results:

<Action name = "login" class = "com. study. struts. action2.LoginAction" method = "login"> <! -- The local configuration for an action is configured in the Action --> <result name = "success">/demo2/result. jsp </result> </action>

Global results:

<! -- Global result view: all actions in the package can use the same view --> <global-results> <! -- <Result name = "success">/index. jsp </result> --> <! -- A. Default result set type --> <result name = "error" type = "dispatcher">/error. jsp </result> <! -- B. forward to another Action --> <! -- <Result name = "success" type = "chain"> hello </result> --> <! -- C. Redirect to the jsp page --> <! -- <Result name = "success" type = "redirect">/index. jsp </result> --> <! -- D. Redirect to action --> <! -- <Result name = "success" type = "redirectAction"> hello </result> --> </global-results>

At the same time, we need to think about a problem. If success is returned but configured globally and locally, which one will be executed?

According to the rules, first check whether the local variables are configured. If the local variables are configured, the local variables are executed. Generally, successful result sets are not configured as global ones, and the global variables may be configured only when an error occurs.

(2) result type

There are 10 common result types configured in struts-default.xml:

 

Of course, in our actual development, there are only 4.5 types of applications. Here I will write 4 types

Example:

 1        <action name="main"> 2             <result type="dispatcher">/main.jsp</result> 3         </action> 4          5         <action name="main2"> 6             <result type="redirect">/main2.jsp</result> 7         </action> 8          9         <action name="main3">10             <result type="chain">main3</result>11         </action>12         13         <action name="main4">14             <result type="redirectAction">main4</result>15         </action>

A)Dispatcher (default)Forward to a JSP, HTML, or other result page. It cannot be an Action.

Request forwarding: The underlying layer calls the forward () method of RequestDispatcher. dispatcher is the default value of the type attribute of result, which is usually used to redirect to a JSP.

B)RedirectRedirect to result view to a URL

Redirection: the data in Action cannot be displayed on the new page. Because the response. sendRedirect ("") method is called at the underlying layer, data within the request range cannot be shared.

C)ChainServer forward to another Action

Forward the action request with the original status to the new action. The two actions share an ActionContext. The actionName specifies the name of the new action to be switched, and the method specifies the method to be switched,

Namespace specifies the namespace of the new Action. If this parameter is left blank, it indicates that the new Action is in the same namespace as the original Action;

D)RedirectActionRedirect to another Action

Redirects to another Action. The parameter is used in the same way as the chain parameter. You can specify a new name for the attribute in the original Action to add it to the new Action, you can add <param name = "B" >$ {a} </param> to the Result tag,

This indicates that the value of variable a in the original Action is transferred to B, and the next Action can be operated by B in the value stack.

(3) What is the difference between redirection and forwarding? 

1. Request forwarding can only forward requests to components in the same WEB application. redirection can also redirect resources in different applications on the same site, or even to an absolute URL.

2. You can view the URL of the target page in redirection mode. You can only view the URL of the first accessed page in forwarding mode. In the future, all the work will be done by the server.

3. The same request object and response object are shared between the request response caller and the called. The redirection caller and the called are two independent access requests and response processes.

4. After redirection, you must add a return statement. Otherwise, even if the page jumps, the following statement will be executed. If the redirection is executed, the following code will not be executed.

(4) Redirect and redirectAction In the result typeFirst, you must know that as long as the request is redirected, all the previously stored items in the request will disappear.
Because redirection actually sends the second request, the content in the request will not appear in the second request.
That is to say, redirection does not share the request. The difference between the following content in the request cannot be received on the redirected page:

Redirect redirects to another physical resource after processing the current Action.
RedirectAction is also a redirection, but it is redirected to another Action

(5) Think about when to use redirection in our actual development?

My personal point of view: After you perform related operations in the request scope, You need to echo the data returned from the database. For example, you need to echo the information when you click Modify, therefore, we recommend that you forward the data because the echo data cannot be obtained through redirection.

In addition, if you want to jump to an external website, such as www.baidu.com, you will not be able to use forwarding, so you can specify redirection. This is the first point mentioned above.

As mentioned in this Article, if there are any deficiencies or you can modify them better, please leave a message.

 

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.