Details of Struts2 configuration

Keywords: Struts xml Apache JSP

1. Execution process chart of struts 2

The whole implementation process is divided into two parts:

  • 1. Server start - > initialization filter - > Load struts.xml
  • 2. Browser send request - > filter - > struts.xml Find action class name in - > instantiate action class

The action class will be instantiated every time the request is sent, so it is found that the action class of struts is multiple. (so after spring integrates struts 2, you also need to configure action classes as multiple examples.)


1.1 implement a simple struts 2 case

1. In web.xml Configure filter in to filter all paths

<!-- to configure sturts Front end controller -->
<filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

2. Preparation 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="22p1" extends="struts-default">
        <action name="hello" class="cn.itcast.action.HelloAction" method="hello">
            <result name="success" type="dispatcher">/success.jsp</result>
        </action>
    </package>
</struts>

3. Write HelloAction

public class HelloAction {
    /**
     * A method to be executed after being requested to a class: action method
     * @return
     */
    public String hello(){
        System.out.println("hello Method executed......");
        return "success";
    }
}

4.index.jsp

<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <!-- struts2 By default, the core filter of*.action Having or without a suffix url request -->
    <a href="${pageContext.request.contextPath }/hello.action">visit hello.action</a><br>
    <a href="${pageContext.request.contextPath }/hello">visit hello</a>
  </body>
</html>

5.success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <h1>Execution succeeded!</h1>
  </body>
</html>

1.2 how to get request, response, session and ServletContext from struts

package cn.itcast.action;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

public class HelloAction implements ServletRequestAware, ServletResponseAware, SessionAware, ServletContextAware {
    private HttpServletRequest httpServletRequest;
    private HttpServletResponse httpServletResponse;
    private Map<String, Object> session;
    private ServletContext servletContext;
    
    public String hello(){
        System.out.println("hello Method executed......");
        return "success";
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.httpServletResponse = httpServletResponse;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}

2. Details of Struts2 configuration file

2.1 six configuration files of struts 2

There are two types of core configuration files of Struts2, six in total. They are described as follows according to the loading order:

Class I: built in configuration file, which cannot be modified.

  • 1.default.properties
    Location: struts 2-core-2.3.24.jar\ org.apache.struts2 Directory
    Function: defines some constants (key value pairs). Some function switches.
  • 2.struts-default.xml
    Location: at the end of the struts 2-core-2.3.24.jar directory
    Function: defines Bean element, result set type, interceptor, etc.
  • 3.struts-plugin.xml
    Location: at the root of the plug-in package
    Function: used for some configuration of extension.

The second category: user-defined configuration files, which can be modified

  • 1.struts.xml
    Location: under src of user project.
    Function: used for configuration of user development, such as action, result, etc.
  • 2.struts.properties
    Location: generally under src of user project.
    Function: can only be used to configure some constants (override built-in default.properties Constant configuration in)
    It's not very useful either. It can be used in struts.xml Direct configuration constant in
  • 3.web.xml
    Location: under WEB-INF directory
    Function: you can configure the core filter of struts 2 and some constants of struts 2 (generally not used to configure constants)
    Note: the loading order here refers to the loading order of constants configured by struts 2, not the whole web.xml Documents, web.xml It will be loaded when tomcat starts.

The reading order of all configuration files is: 1 default.properties —2, struts-default.xml —3, struts-plugin.xml —4, struts.xml —5, struts.properties —6, web.xml Constant configuration in
Of course, the configuration of constants is not struts.properties and web.xml To configure in struts.xml It can be configured in.


These loading sequences are all reflected in the core controller of struts 2. The source code is as follows:

In the init method, the initDispatcher method of init is called to load the configuration file and enter the code:

We will find that this method calls dispatcher's init method again. Enter the init method:

This series of code is used to load the configuration file of struts 2.


2.2 precautions for configuration file

1. Struts 2 provides three ways to configure constants.
One way is to use key=value, which is to use the. properties file.
One is in struts.xml Configuration in.
One is in web.xml Configuration in.

We recommend using struts.xml file

2. When multiple configuration files have the same constant configuration, the one who loads the configuration file will configure the constant in the previous configuration file to overwrite.


2.3 common constants in struts 2

Constant defined in default.properties In the configuration file, the expression is key=value. All struts 2 applications use these constants.

because struts.action.extension The default value is action,,, so hello.action And hello to enter the struts 2 Framework

Test: change to do
here hello.action And hello
Instead, do allows hello and hello.do


Three struts.xml Label details in

constant tag

effect:
	Used to modify constants in struts 2
 Properties:
	name: key of specified constant
	Value: Specifies the value of a constant
 Usage:
<! -- Open developer mode -- >
<constant name="struts.devMode" value="true"></constant>

package label

effect:
	In the configuration file of struts 2, the idea of object-oriented is introduced and subcontract management is used. It can be used to manage action classes with different functions. It is convenient for modular development of action class.
Properties:
	name: 
		The name of the package. Must write. And must be unique.
	extends: 
		In general, you need to inherit the struts default package. However, if you do not inherit, you will not be able to use the core functions provided by struts 2. struts-default.xml Struts default is defined in. And struts-default.xml It's in our struts.xml Load before loading.

	namespace: 
		Namespace. Its function is to manage the access URL according to the modularity.
		How to write a namespace:
			  Must start with /
			  It can be followed by a combination of letters and numbers, or just letters.
			  When we specify a namespace, the URL we access becomes:
								name attribute value of namespace + action tag
			  For example:
				/n1/hello.action
			  	/user/addUser.action
			  	/user/updateUser.action
			  	/product/findProductList.action
			  	/product/addProduct.action
		The default value for the namespace is (provided in the official document of struts 2) index.html-Guides)
Usage:
<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="p1" extends="struts-default" namespace="/user">
		<action name="hello" class="cn.itcast.struts.HelloAction" method="hello">			
			<result name="success" type="dispatcher">/success.jsp</result>
		</action>
	</package>
</struts>

Posted by Ju-Pao on Fri, 05 Jun 2020 02:32:41 -0700