stay struts2 There are tags in the core configuration file , This tab sets the result processing jump after the method is run , The result may be a jump to a jsp、html page 、 Other action Or pass a value, etc , And this chapter is aimed at struts2 The result is to deal with jump learning ~



Struts2 Result jump mode



1、Struts2 The way the results jump

Struts2 Processing of the results ( both :struts.xml Configured in the file result Of type attribute ), The processing method is a jump method , Jump to jsp Page or Action Class , There are four ways to jump , Namely (dispatcher Default )、(redirect)、(chain)、(redirectAction) These four ;
stay struts2 There are many ways to jump in , The configuration information is shown in the figure below , In the big red box is type The attribute value , There are more than a dozen , But the most commonly used are the four species of the genus above , If you forget the attribute value, you can enter struts.default View in file ;
struts.default Jump mode configuration information in the file
Here are the detailed learning records of these four jump methods ~
First :
One jsp page

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Test</title>
</head>
<body>
<h1>Hello!You are right!</h1>
</body>
</html>


1.1、 forward

Forwarding is struts2 A default return value jump setting in the frame , It was mentioned in the previous main tab that , The configuration code is as follows :

Demo1Action

package com.java.Demo;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport{

	public String execute() throws Exception {
		System.out.println("Demo1Action");
		return "success";
	}
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
   
   <package name="result" namespace="/" extends="struts-default">
   <!--  Forward jump page  -->
   <action name="Demo1Action" class="com.java.Demo.Demo1Action" method="execute">
      <result name="success" type="dispatcher">/hello.jsp</result>
   </action>
   </package>
</struts>

Forwarding is the default , So you don't have to write this configuration ;
After configuring and running, the method name will still be displayed in the path , As shown below :
 Running results


1.2、 Redirect

To redirect, you need to set the property value of the property to , The difference from forwarding is that the access path is displayed in a different style at the end , Since the address bar information display is different , Redirection is the final location to the jump page , The address bar information is as follows :
 Insert picture description here


1.3、 Forwarding to Action

Advance declaration , This method is not commonly used , Its configuration is different from the above two , Because it's forwarded to Action Class , So we need to have action The name of the class and the path of the class , Its configuration is as follows :

Demo3Action

package com.java.Demo;

import com.opensymphony.xwork2.ActionSupport;

public class Demo3Action extends ActionSupport{

	public String execute() throws Exception {
		System.out.println("Demo3Action");
		return "success";
	}
	
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
   
   <package name="result" namespace="/" extends="struts-default">
   <!--  Forwarding to action -->
   <action name="Demo3Action" class="com.java.Demo.Demo3Action" method="execute">
      <result name="success" type="chain">
      <!--  Forward to action The name of the class  -->
      <param name="actionName">Demo1Action</param>
      <!--  Forward to action Path to class  -->
      <param name="namespace">/</param>
      </result>
   </action>
   </package>
</struts>

Add two tags to , The attribute names are , But one of its attribute values is actionName One is namespace, They are class names 、 Classpath settings ;
Two action The order in which classes are loaded is from the outermost layer , To the innermost layer , After running, the address bar doesn't change , Consistent with forwarding , The running results are as follows :
 Forwarding to Action Realize the figure


1.4、 Redirect to Action

This method is very common ~
Its configuration operation and forwarding to action Similar , Its attribute value is , The key configuration is to add two sub tags to the tag , Its properties and values are also forwarded to Action The same as , Its configuration is as follows :

Demo4Action

package com.java.Demo;

import com.opensymphony.xwork2.ActionSupport;

public class Demo4Action extends ActionSupport{

	public String execute() throws Exception {
		System.out.println("Demo4Action");
		return "success";
	}
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
   
   <package name="result" namespace="/" extends="struts-default">
   <!--  Redirect to action -->
   <action name="Demo4Action" class="com.java.Demo.Demo4Action" method="execute">
      <result name="success" type="redirectAction">
      <!--  Redirected to action The name of the class  -->
      <param name="actionName">Demo1Action</param>
      <!--  Redirect to action Path to class  -->
      <param name="namespace">/</param>
      </result>
   </action>
   </package>
</struts>

The running results are as follows :
 Redirect to action Running results
Redirect to action And forward to action It's the same , Except that the address bar will change ;


《END》