Struts 2 Learning Notes of J2EE Series (8) -- struts.xml Modular Configuration

Keywords: Struts xml Apache encoding

Let's first look at the configuration file of the project in the previous blog:


There are two packages in this file, or two modules. When a project is relatively large, there may be dozens or even hundreds of modules in it. If they all write struts.xml files like this, then struts.xml will inevitably be very confused. Struts 2 provides a modular configuration method. Let's look at an example:

1. Second Learning Struts 2Chap02_04: Suppose there are two big modules under this project: Vehicle Management and Asset Management.

2. Create a new cheliang.xml file under the src directory as the configuration file of the vehicle management module:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	
   <package name="cheliang" namespace="/cheliang" extends="struts-default">
   		<action name="cheliang" class="com.test.action.CheLiangAction">
   			<result name="success">${pageContext.request.contextPath}/success.jsp</result>
   		</action>
   </package>

</struts>

New zichan.xml as the configuration file of the asset management module:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	
   <package name="zichan" namespace="/zichan" extends="struts-default">
   		<action name="zichan" class="com.test.action.ZiChanAction">
   			<result name="success">${pageContext.request.contextPath}/success.jsp</result>
   		</action>
   </package>

</struts>

These two configuration files are included in the struts.xml file by the <include file="></include> tag:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	
   <include file="cheliang.xml"></include>
   <include file="zichan.xml"></include>

</struts>

In this way, we configure the configuration files of the two modules of vehicle management and asset management into the struts.xml file.

3. New CheLiangAction:

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class CheLiangAction extends ActionSupport{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		
		System.out.println("Implemented CheLiangAction The default method");
		
		return SUCCESS;
	}
	

}

New ZiChanAction:

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class ZiChanAction extends ActionSupport{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		
		System.out.println("Implemented ZiChanAction The default method");
		
		return SUCCESS;
	}
	

}

Running procedures:


The console output is:



The program runs successfully.

To sum up, the sub-module configuration of struts2 project is realized by <include file="> </include>.

Posted by bennyboywonder on Thu, 14 Feb 2019 17:00:18 -0800