Jumping mode of struts 2

Posted by mrausch on Sat, 29 Jun 2019 23:09:19 +0200

There are four jump modes in struts 2

1. dispatcher. Request forwarding (default), only jumping to jsp page
 2. redirect to jsp
 3. redirectAction
 4. chain forward to action

Before that, let's talk about the concept of request forwarding and redirection. If you already know about Forwarding and redirection, you can skip it and look directly at the following.

Forwarding and redirection

 1. Forwarding is done between servers

   Redirection is when a user sends a new request back to the server

 2. The forwarding address bar will not change

   The address bar will change because the redirection is a new request that is re-sent.

 3. Forwarding shares a scope object, which is equivalent to adding the content of A.jsp page to B.jsp page.

   Redirect, and you get a message object

 4. Forwarding is faster than redirection (because redirection resends requests)

When to use forwarding and redirection?

   There are two pages, one is data transfer, the other is request forwarding, the other is redirection.

   For example, when a servlet queries data that needs to be displayed on a page, it forwards it with a request.

   For example, the servlet does an update operation and jumps to other pages. Use redirection.

Now let's get to the point. Let's first look at the page effect and the structure of the project.

  1. Project structure

    

2. Look at the effect map of the page, first the page is boring, then add a background picture. Remember to revise it yourself.

 

Let's take a look at the details of the implementation. Don't be obscured by mystery. In fact, it's not mysterious.

  1. userIndex.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	/* entry name */
	String path = request.getContextPath();
	// Acquisition transport protocol  
	String http = request.getScheme();
	// Get the server name
	String serverName = request.getServerName();
	// Get the server port number
	Integer serverPort = request.getServerPort();
	// Spliced project URL s
	String bath = http + "://" + serverName + ":" + serverPort + path;
	System.out.println(bath);
%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body style="background-image: url('image/bg1.jpg');">
<!-- Jump mode -->
<a href="<%=bath %>/jump/jumpDipatcher?param=1">dispatcher Default mode,Request forwarding</a><br/>
<a href="<%=bath %>/jump/jumpDipatcher?param=2">redirect Redirect to jsp page</a><br/>
<a href="<%=bath %>/jump/jumpDipatcher?param=3">redirectAction Redirect to Action</a><br/>
<a href="<%=bath %>/jump/jumpDipatcher?param=4">chain Forwarding to Action</a><br/>
</body>
</html>

2. For login.jsp and error.jsp and success.jsp. In fact, there is no content, just a page, not written here.

 But when you test, you have to write, of course, the name can also be written by yourself (after all, to test, not formal development).

3.TestJump

package com.struts2.control;

/**
 * @author admin
 * Test jump mode
 */
public class TestJump {
	
	private String param; 
	
	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public String testDispatcher(){
		if("1".equals(param)){
			System.out.println("-----Dispatcher--------");
			return "dispatcher";
		}else if("2".equals(param)){
			System.out.println("-----Redirect--------");
			return "redirect";
		}else if("3".equals(param)){
			System.out.println("-----RedirectAction--------");
			return "redirectAction";
		}else if("4".equals(param)){
		        System.out.println("-----Chain--------");
			return "chain";
		}
		return null;
	}
	public String returnUserIndex(){
		return "userIndex";
	}
	public String returnChain(){
		return "chain";
	}
}


4. Look at how struts.xml is configured. The veil will be unveiled soon. If you don't know about the properties in the configuration, please refer to the previous chapter.

 http://11144439.blog.51cto.com/11134439/1926477 


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">   
   
<struts>
	<package name="jumps" extends="struts-default" namespace="/jump">
		<action name="jumpDipatcher" class="com.struts2.control.TestJump" method="testDispatcher">
			<!-- Request forwarding,Jump To jsp page -->
			<result name="dispatcher" type="dispatcher">/success.jsp</result>
			<!-- Redirect to jsp page -->
			<result name="redirect" type="redirect">/success.jsp</result>
			<!-- Redirect to Action -->
			<result name="redirectAction" type="redirectAction">
			        <!-- Here's a jump to Action Of package Of namespace,
			           Be careful  / Don't neglect it.
			         -->
				<param name="namespace">/jump</param>
				<!-- This is action Medium name Look at the picture below. -->
				<param name="actionName">returnUserIndex</param>
			</result>
			<!-- Forwarding to chain -->
			<result name="chain" type="chain">
				<param name="namespace">/jump</param>
				<param name="actionName">returnChain</param>
			</result>
		</action>
		<action name="returnUserIndex" class="com.struts2.control.TestJump" method="returnUserIndex">
			<result name="userIndex" type="redirect">/login.jsp</result>
		</action>
		<action name="returnChain" class="com.struts2.control.TestJump" method="returnChain">
			<result name="chain" type="redirect">/login.jsp</result>
		</action>
	</package>
</struts>

This is the configuration diagram in forwarding action (the same is true for redirectAction redirection to Action)



5. Did you find anything in the configuration above? Does it feel like jumping to the same page? Yes, look at the picture below.

If you follow the above configuration, does it feel that the code has been reduced again, no mistake? Here is the configuration. Remember to modify the return string in Action to be consistent with the global one.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">   
   
<struts>
	<package name="jumps" extends="struts-default" namespace="/jump">
		<!-- Global jump,Extract repetitive result,Simplified configuration -->
		<global-results>
			<result name="success">/success.jsp</result>
			<result name="login">/login.jsp</result>
		</global-results>
		
		<action name="jumpDipatcher" class="com.struts2.control.TestJump" method="testDispatcher">
			<!-- Redirect to Action -->
			<result name="redirectAction" type="redirectAction">
				<param name="namespace">/jump</param>
				<param name="actionName">returnUserIndex</param>
			</result>
			<!-- Forwarding to chain -->
			<result name="chain" type="chain">
				<param name="namespace">/jump</param>
				<param name="actionName">returnChain</param>
			</result>
		</action>
		<action name="returnUserIndex" class="com.struts2.control.TestJump" method="returnUserIndex">
		</action>
		<action name="returnChain" class="com.struts2.control.TestJump" method="returnChain">
		</action>
	</package>
</struts>


6. Having said so much, let's test the results.

  1. dispacher request forwarding jump. Test figure

  2. Redirect redirect jsp, the effect is as follows

  3. redirectAction redirects to Action, as shown below


4. chain forwards to Action, as shown in Figure 1

To conclude here, it is important to do it by yourself, by yourself and by yourself. Well, important things have been said three times. Close the work and build a daughter-in-law back home.


Topics: Java JSP Struts Apache xml