Detailed example of configuring struts.xml
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
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>
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>
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>
3,
In In Struts2
framework, action, result,
interceptor, interceptor-stack and other configuration information are managed through packages. The package attributes are as follows:
Is it required | Description | name | ||||||||||||||||||||||||||||||||||||
package name, as | otherpackages apply the tag of this package | extends | ||||||||||||||||||||||||||||||||||||
Set this package | InheritOther packages | namespace | ||||||||||||||||||||||||||||||||||||
Set the | namespace of the packageabstact | |||||||||||||||||||||||||||||||||||||
Set to abstract Bag |
|
属性名称 | 是否必须 | 功能描述 |
name | 是 | 请求的Action名称 |
class | 否 | Action处理类对应具体路径 |
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"; } }
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>
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>
6、
当我们在配置Action的时候,如果没有为某个Action指定具体的class值时,系统将自动引用
有特殊需要时,可以手动指定默认的class
package wwfy.action; public class DefaultClassRef { public void execute(){ System.out.println("默认class开始执行……"); } }
在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>
7、
如果在请求一个没有定义过的Action资源时,系统就会抛出404错误。这种错误不可避免,但这样的页面并不友好。我们可以使用
<?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>
8、
该标签用来设置整个包范围内所有Action所要应用的默认拦截器信息。事实上我们的包继承了struts-default包以后,使用的是Struts的默认设置。我们可以在struts-default.xml中找到相关配置:
<default-interceptor-ref name="defaultStack"/>
在实际开发过程中,如果我们有特殊的需求是可以改变默认拦截器配置的。当时一旦更改这个配置,“defaultStack”将不再被引用,需要手动最加。
9、
通过该标签可以向Struts2框架中注册拦截器或者拦截器栈,一般多用于自定义拦截器或拦截器栈的注册。该标签使用方法如下:
<interceptors> <interceptor name="拦截器名" class="拦截器类"/> <interceptor-stack name="拦截器栈名"> <interceptor-ref name="拦截器名"> </interceptor-stack> </interceptors>
10、
通过该标签可以为其所在的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>
【相关推荐】
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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

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 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

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 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 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 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
