The difference between the redirect,redirectaction,chain of Struts2 result type

Source: Internet
Author: User

First, the introduction

This article describes the type attribute value of result in the STRUTS2 configuration file: The difference between redirect,redirectaction,chain.

First of all, briefly introduce the meaning of these three.

1 redirect:action after processing, redirect to a view resource (such as: JSP page), the request parameters are all lost, action processing results are all lost.
2 redirectaction:action after processing, redirect to an action, the request parameters are all lost, action processing results are all lost.
3 chain:action after processing, forwarding to an action, the request parameters are all lost, action processing results will not be lost.


Second, testing

Here we start to build test cases.

Here we take the user registration as an example, two action to simulate the above three types of request forwarding test.


1. To create the user class, because the purpose of this example is to test three kinds of request forwarding methods, the user class is designed to be simpler.

Package org.xjh.domain;

public class User {
	private String username;
	private String password;
	
	Public String GetUserName () {return
		username;
	}
	public void Setusername (String username) {
		this.username = username;
	}
	Public String GetPassword () {return
		password;
	}
	public void SetPassword (String password) {
		this.password = password;
	}
	
	@Override public
	String toString () {return
		"User [username=" + Username + ", password="
				+ password + "]" c19/>}
}

2. The creation of the Action1,action1 will receive a registration request from the user on the registration page (of course this is used only as a simulation and does not operate the database)

Package org.xjh.struts.action;
Import Org.apache.struts2.ServletActionContext;

Import Org.xjh.domain.User;

public class Action1 {    private user user;     public User GetUser () {        return user;     }   

   public void SetUser (user user) {        this.user = user;     }     public String Execute () {        //in order to differentiate, we add the _first tag in the back,
To indicate that this is the data received by Action1         user.setusername (user.getusername () + "_first");
        user.setpassword (User.getpassword () + "_first");
        system.out.println (user);
        system.out.println ("hashcode =" + User.hashcode ());         //Deposit Prompt information to determine if Redirect,redirectaction,chain forwarding request is missing data         servletactioncontext.getrequest (). setattribute ("message", "registered successfully.")
");
        return "Success";

    &nbsp}}

3. Create Action2 Class

Package org.xjh.struts.action;

Import Org.apache.struts2.ServletActionContext;
Import Org.xjh.domain.User;

public class Action2 {

	private user user;

	Public User GetUser () {return
		user;
	}
	public void SetUser (user user) {
		this.user = user
	}

	Public String Execute () {
		//to differentiate, we added _second This tag later to indicate that this was the data received by Action2
		User.setusername ( User.getusername () + "_second");
		User.setpassword (User.getpassword () + "_second");
		SYSTEM.OUT.PRINTLN (user);
		System.out.println ("Second User hashcode =" + User.hashcode ());
		A message is stored to determine whether the Redirect,redirectaction,chain forwarding request is missing data
		servletactioncontext.getrequest (). setattribute (" Message "," registered successfully. ");
		Return "Success";
	}



4. Create a registration page register.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

5. Message Prompt page

<%@ page import= "java.util.*" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

6. Configure action to process registration requests

Here we first test result type= "Redirect", that is, redirect forwarding request.

<package name= "User" namespace= "/" extends= Struts-default "> <action name=" firstaction "
	class=" Org.xjh.struts.action.Action1 ">
	     <result name=" Success "type=" redirect ">/message.jsp</result>
	</action>
	<action name= "secondaction" class= "Org.xjh.struts.action.Action2" >
	     < Result name= "Success" >/message.jsp</result>
	</action>
</package>


7. Test

1) test redirect forwarding request

Open the browser and enter the URL to access the Register.jsp page in the address bar

Username and password are entered separately: test, 123. Click the Register button.


You will notice that the URL of the address bar has changed, which indicates that the redirect is a client-redirected way to jump.


At the same time in the MyEclipse console window, you will see the following message:




2) test redirectaction forwarding Request

Modify struts's configuration file as follows:

<package name= "User" namespace= "/" extends= Struts-default "> <action name=" firstaction "
	class=" Org.xjh.struts.action.Action1 ">
		<result name=" Success "type=" Redirectaction ">
			<param name=" Namespace ">/</param>
			<param name=" ActionName ">secondAction</param>
			<param name=" User.username ">${user.username}</param>
			<param name=" User.password ">${user.password}</ param>
		</result>	
	</action>
	<action name= "secondaction" class= " Org.xjh.struts.action.Action2 ">
		<result name=" Success ">/message.jsp</result>
	</ Action>
</package>

Return to the registration page, username and password entered separately: Test, 123. Click the Register button.


You will find that the URL of the address bar becomes the URL of the Aciton1 request Action2, which indicates that the redirectaction is redirected by the client, and the browser page is prompted with the following message:


At the same time in the MyEclipse console window, you will see the following message:

This indicates that the second action (Action2) Gets the user information from the user information processed by the first action (Action1), and that the addresses of these 22 objects are different (hashcode different)


3) test chain

Modify struts's configuration file as follows:

<package name= "User" namespace= "/" extends= Struts-default "> <action name=" firstaction "
	class=" Org.xjh.struts.action.Action1 ">	
		<result name=" Success "type=" Chain "> <param name=" namespace "
			>/</param>
			<param name= "ActionName" >secondAction</param>
		</result>
	</ action>
	<action name= "secondaction" class= "Org.xjh.struts.action.Action2" >
		<result name= " Success ">/message.jsp</result>
	</action>
</package>


Return to the registration page, username and password entered separately: Test, 123. Click the Register button.


You will find that the URL of the address bar only becomes the URL of the request Action1, which indicates that the chain is a service side directed way, and the following message is given in the browser page:


At the same time in the MyEclipse console window, you will see the following message:


As can be seen from the above information, the user object information processed by Action1 is not passed directly to Action2, the request data submitted by the JSP page is reinstalled into a user object, and the user object

Referenced by the user variable in the Action1, and also by the Action2 user variable, which is the same as their hashcode.


Third, summary

REDIRECT represents client redirection, which is used to redirect to a page.

Redirectaction also represents client redirection, which is used to redirect to an action

Chain represents a service-side request forwarding, which is used to share data information between multiple action.

Redirect and redirecaction are similar to Response.sendredirect (URLs);

Chain is similar to Request.getrequestdispatcher (URL). Forward (request, response);

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.