Table of Contents
For example, in an online shopping program, user configuration, product configuration, and order configuration can be placed in three configuration files user.xml, goods.xml, and order.xml respectively, and then these are placed in struts.xml. 3 configuration files are introduced:
defined in the struts.properties file can be configured in the struts.xml file. In struts.xml, it is configured through the <constant> tag:
In In Struts2
, action, result,

2、extends属性的详解
3、namespace的详解
1)如果使用命名空间则URL将改变
2)默认命名空间
3)指定根命名空间
4、<action>与<result>
1、<action>属性介绍
2、<result>属性介绍
通配符的使用" >
3、通配符的使用
4、访问Action方法的另一种实现方式
5、<exception-mapping>与<global-exception-mapping>
6、<default-class-ref>
7、<default-action-ref>
8、<default-interceptor-ref>
9、<interceptors>
10、<interceptor-ref>
11、<global-results>
Home Backend Development XML/RSS Tutorial Detailed example of configuring struts.xml

Detailed example of configuring struts.xml

May 04, 2017 pm 03:53 PM

struts.xml is the most utilized file in our development and the most important configuration file in Struts2.

Let’s introduce several tags commonly used in struts. Split into multiple configuration files, and then use the tag in struts.xml to introduce other configuration files.

For example, in an online shopping program, user configuration, product configuration, and order configuration can be placed in three configuration files user.xml, goods.xml, and order.xml respectively, and then these are placed in struts.xml. 3 configuration files are introduced:

struts.xml:

<?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="user.xml"/>
	<include file="goods.xml"/>
	<include file="order.xml"/>
</struts>
Copy after login

user.xml:

<?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="wwfy" extends="struts-default">
		<action name="login" class="wwfy.user.LoginAction">
			<!--省略Action其他配置-->
		</action>
		<action name="logout" class="wwfy.user.LogoutAction">
			<!--省略Action其他配置-->
		</action>
	</package>
</struts>
Copy after login

2,

mentioned before In the introduction of the struts.properties configuration file, we mentioned that all

properties

defined in the struts.properties file can be configured in the struts.xml file. In struts.xml, it is configured through the 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>
	<!--设置开发模式-->
	<constant name="struts.devMode" value="true"/>
	<!--设置编码形式为GB2312-->
	<constant name="struts.i18n.encoding" value="GB2312"/>
	<!--省略其他配置信息-->
</struts>
Copy after login

3, 1, Package attribute introduction

In In Struts2

framework

, action, result,

interceptor

, interceptor-stack and other configuration information are managed through packages. The package attributes are as follows:

Attribute is the other NoInheritNo namespace of the packageNo

Is it required

Description

name
package name, as packages apply the tag of this packageextends
Set this packageOther packagesnamespace
Set the abstact
Set to abstract Bag


2、extends属性的详解

  • 当一个包通过配置extends属性继承了另一个包的时候,该包将会继承父包中所有的配置,包括action、result、interceptor等。

  • 由于包信息的获取是按照配置文件的先后顺序进行的,所以父包必须在子包之前被定义。

  • 通常我们配置struts.xml的时候,都继承一个名为“struts-default.xml”的包,这是struts2中内置的包。

3、namespace的详解

namespace主要是针对大型项目中Action的管理,更重要的是解决Action重名问题,因为不在同一个命名空间的Action可以使用相同的Action名的。

1)如果使用命名空间则URL将改变

比如我们有一下配置文件

<package name="wwfy" extends="struts-default">
	<action name="login" class="wwfy.action.LoginAction">
		<result>/success.jsp</result>
	</action>
</package>
Copy after login

则此配置下的Action的URL为localhost:8080/login.action

假如为这个包指定了命名空间

<package name="wwfy" extends="struts-default" namespace="/user">
	<action name="login" class="wwfy.action.LoginAction">
		<result>/success.jsp</result>
	</action>
</package>
Copy after login

则此配置下的Action的URL为localhost:8080/user/login.action

2)默认命名空间

Struts2中如果没有为某个包指定命名空间,该包使用默认的命名空间,默认的命名空间总是""。

3)指定根命名空间

当设置了命名空间为“/”,即指定了包的命名空间为根命名空间时,此时所有根路径下的Action请求都会去这个包中查找对应的资源信息。

假若前例中路径为http://localhost:8080/login.action则所有http://localhost:8080/*.action都会到设置为根命名空间的包中寻找资源。

4、

1、属性介绍

属性名称

是否必须

功能描述

name请求的Action名称
classAction处理类对应具体路径
method指定Action中的方法名
converter指定Action使用的类型转换

如果没有指定method则默认执行Action中的execute方法。

2、属性介绍

属性名称

是否必须

功能描述

name对应Action返回逻辑视图名称,默认为success
type返回结果类型,默认为dispatcher


3、通配符的使用

随着result的增加,struts.xml文件也会随之变得越来越复杂。那么就可以使用通配符来简化配置:

例如下面这个案例:

Action为Test.java

public class Test {
	public String test1(){
		return "result1";
	}
	
	public String test2(){
		return "result2";
	}
	
	public String test3(){
		return "result3";
	}
}
Copy after login

struts.xml中配置为

<package name="wwfy" extends="struts-default">
	<action name="test*" class="wwfy.action.test{1}">
		<result name="result{1}">/result{1}.jsp</result>
	</action>
</package>
Copy after login

4、访问Action方法的另一种实现方式

在Struts2中如果要访问Action中的指定方法,还可以通过改变URL请求来实现,将原本的“Action名称.action”改为“Action名称!方法名称.action”在struts.xml中就不需要指定方法名了。

5、

这两个标签都是用来配置发生异常时对应的视图信息的,只不过一个是Action范围的,一个是包范围的,当同一类型异常在两个范围都被配置时,Action范围的优先级要高于包范围的优先级.这两个标签包含的属性也是一样的:

属性名称

是否必须

功能描述

name用来表示该异常配置信息
result指定发生异常时显示的视图信息,这里要配置为逻辑视图
exception指定异常类型


两个标签的示例代码为:

<?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="default" extends="struts-default">
		<global-exception-mappings>
			<exception-mapping result="逻辑视图" exception="异常类型"/>
		</global-exception-mappings>
		<action name="Action名称">
			<exception-mapping result="逻辑视图" exception="异常类型"/>
		</action>
	</package>
</struts>
Copy after login

6、

当我们在配置Action的时候,如果没有为某个Action指定具体的class值时,系统将自动引用标签中所指定的类。在Struts2框架中,系统默认的class为ActionSupport,该配置我们可以在xwork的核心包下的xwork-default.xml文件中找到。

有特殊需要时,可以手动指定默认的class

package wwfy.action;

public class DefaultClassRef {
	public void execute(){
		System.out.println("默认class开始执行……");
	}
}
Copy after login

在struts.xml中配置

<?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="wwfy" extends="struts-default">
		<!-- 指定默认class为Test -->
		<default-class-ref class="wwfy.action.DefaultClassRef"/>
		<action name="test1">
			<result>/index.jsp</result>
		</action>
	</package>
</struts>
Copy after login

7、

如果在请求一个没有定义过的Action资源时,系统就会抛出404错误。这种错误不可避免,但这样的页面并不友好。我们可以使用来指定一个默认的Action,如果系统没有找到指定的Action,就会指定来调用这个默认的Action。

<?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="wwfy" extends="struts-default">
		
		<default-action-ref name="acctionError"></default-action-ref>
		<action name="acctionError">
			<result>/jsp/actionError.jsp</result>
		</action>
	</package>
</struts>
Copy after login

8、

该标签用来设置整个包范围内所有Action所要应用的默认拦截器信息。事实上我们的包继承了struts-default包以后,使用的是Struts的默认设置。我们可以在struts-default.xml中找到相关配置:

<default-interceptor-ref name="defaultStack"/>
Copy after login

在实际开发过程中,如果我们有特殊的需求是可以改变默认拦截器配置的。当时一旦更改这个配置,“defaultStack”将不再被引用,需要手动最加。

9、

通过该标签可以向Struts2框架中注册拦截器或者拦截器栈,一般多用于自定义拦截器或拦截器栈的注册。该标签使用方法如下:

<interceptors>
	<interceptor name="拦截器名" class="拦截器类"/>
	<interceptor-stack name="拦截器栈名">
		<interceptor-ref name="拦截器名">
	</interceptor-stack>
</interceptors>
Copy after login

10、

通过该标签可以为其所在的Action添加拦截器功能。当为某个Action单独添加拦截器功能后,中所指定的拦截器将不再对这个Action起作用。

11、

该标签用于设置包范围内的全局结果集。在多个Action返回相同逻辑视图的情况下,可以通过标签统一配置这些物理视图所对应的逻辑视图。

<?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="wwfy" extends="struts-default">
<global-results>
<result name="test">/index.jsp</result>
</global-results>
</package>
</struts>
Copy after login

【相关推荐】

1. XML免费视频教程

2. XML技术手册

3. 李炎恢XHTML视频教程

The above is the detailed content of Detailed example of configuring struts.xml. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles