Struts 2 简明教程
Struts 2 - Configuration Files
这一章将指导你完成 Struts 2 应用程序的基础配置。我们将在这里看到使用几个重要的配置文件,例如 web.xml, struts.xml, strutsconfig.xml 和 struts.properties ,可以配置哪些内容
This chapter will take you through basic configuration which is required for a Struts 2 application. Here we will see what can be configured with the help of few important configuration files like web.xml, struts.xml, strutsconfig.xml and struts.properties
老实说,你只需要使用 web.xml 和 struts.xml 配置文件就可以开始工作(正如你在我们前一章中已经亲眼所见,我们的示例是使用这两个文件进行工作的)。但是,为了你的知识,我们将解释关于其他文件的情况。
Honestly speaking, you can start working by just using web.xml and struts.xml configuration files (as you have already witnessed in our previous chapter where our example worked using these two files). However, for your knowledge we will explain regarding other files also.
The web.xml File
web.xml 配置文件是一个 J2EE 配置文件,它确定 HTTP 请求的元素将如何由 servlet 容器处理。它并不是一个严格意义上的 Struts2 配置文件,但是它是 Struts2 能够工作的需要配置的一个文件。
The web.xml configuration file is a J2EE configuration file that determines how elements of the HTTP request are processed by the servlet container. It is not strictly a Struts2 configuration file, but it is a file that needs to be configured for Struts2 to work.
正如前面讨论的,这个文件提供了一个任何网络应用程序的入口点。Struts2 应用程序的入口点将是一个在部署描述符(web.xml)中定义的过滤器。因此,我们将在 web.xml 中定义一个 FilterDispatcher 类的入口。web.xml 文件需要在文件夹 WebContent/WEB-INF 中被创建。
As discussed earlier, this file provides an entry point for any web application. The entry point of Struts2 application will be a filter defined in deployment descriptor (web.xml). Hence we will define an entry of FilterDispatcher class in web.xml. The web.xml file needs to be created under the folder WebContent/WEB-INF.
如果您在没有模板或生成它的工具(例如 Eclipse 或 Maven2)的帮助下开始,那么这是您需要配置的第一个配置文件。
This is the first configuration file you will need to configure if you are starting without the aid of a template or tool that generates it (such as Eclipse or Maven2).
以下是我们在上一个示例中使用的 web.xml 文件的内容。
Following is the content of web.xml file which we used in our last example.
<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id = "WebApp_ID" version = "3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
请注意,我们把 Struts 2 过滤器映射到 / ,而不是映射到 */ .action*,这意味着所有的 url 都将由 struts 过滤器解析。当我们学习注释章节的时候,我们将涉及到这一点。
Note that we map the Struts 2 filter to /*, and not to /.action* which means that all urls will be parsed by the struts filter. We will cover this when we will go through the Annotations chapter.
The Struts.xml File
struts.xml 文件包含您将在动作开发时修改的配置信息。这个文件可以用来覆盖应用程序的默认设置,例如 struts.devMode = false 和在属性文件中定义的其他设置。这个文件可以创建在 WEB-INF/classes 文件夹中。
The struts.xml file contains the configuration information that you will be modifying as actions are developed. This file can be used to override default settings for an application, for example struts.devMode = false and other settings which are defined in property file. This file can be created under the folder WEB-INF/classes.
让我们来看看在上一个章节中解释的 Hello World 示例中创建的 struts.xml 文件。
Let us have a look at the struts.xml file we created in the Hello World example explained in previous chapter.
<?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" />
<package name = "helloworld" extends = "struts-default">
<action name = "hello"
class = "com.tutorialspoint.struts2.HelloWorldAction"
method = "execute">
<result name = "success">/HelloWorld.jsp</result>
</action>
<-- more actions can be listed here -->
</package>
<-- more packages can be listed here -->
</struts>
首先要注意的是 DOCTYPE 。所有 struts 配置文件都需要有正确的 doctype,正如我们的小示例中所示。 <struts> 是根标签元素,我们在它的下面使用 <package> 标签来声明不同的包。这里 <package> 允许配置的分离和模块化。当您有一个大项目且项目被分成不同的模块时,这将非常有用。
The first thing to note is the DOCTYPE. All struts configuration file needs to have the correct doctype as shown in our little example. <struts> is the root tag element, under which we declare different packages using <package> tags. Here <package> allows separation and modularization of the configuration. This is very useful when you have a large project and project is divided into different modules.
例如,如果您的项目有三个域 - business_application、customer_application 和 staff_application,那么您可以创建三个包并将关联的动作存储在适当的包中。
For example, if your project has three domains - business_application, customer_application and staff_application, then you could create three packages and store associated actions in the appropriate package.
package 标签具有以下属性 −
The package tag has the following attributes −
Sr.No |
Attribute & Description |
1 |
name (required) The unique identifier for the package |
2 |
extends Which package does this package extend from? By default, we use struts-default as the base package. |
3 |
abstract If marked true, the package is not available for end user consumption. |
4 |
namespace Unique namespace for the actions |
constant 标记连同 name 和 value 属性应该用于覆盖 default.properties 中定义的以下任何属性,例如我们刚刚设置的 struts.devMode 属性。设置 struts.devMode 属性使我们能够在日志文件中看到更多调试消息。
The constant tag along with name and value attributes should be used to override any of the following properties defined in default.properties, like we just set struts.devMode property. Setting struts.devMode property allows us to see more debug messages in the log file.
我们定义 action 标记对应于我们想要访问的每个 URL,并且我们定义一个具有 execute() 方法的类,该方法将在我们每次访问相应 URL 时访问。
We define action tags corresponds to every URL we want to access and we define a class with execute() method which will be accessed whenever we will access corresponding URL.
结果确定在执行某个动作后返回到浏览器的内容。从动作返回的字符串应该是结果的名称。结果像上面那样按每个动作配置,或者作为一个“全局”结果,可供包中的每个动作使用。结果具有可选的 name 和 type 属性。默认名称值为 “success”。
Results determine what gets returned to the browser after an action is executed. The string returned from the action should be the name of a result. Results are configured per-action as above, or as a "global" result, available to every action in a package. Results have optional name and type attributes. The default name value is "success".
随着时间的推移,Struts.xml 文件可能会变大,因此按包对其进行拆分是一种模块化方法,但 Struts 提供了另一种模块化 struts.xml 文件的方法。你可以将文件拆分为多个 xml 文件,并按以下方式导入它们。
Struts.xml file can grow big over time and so breaking it by packages is one way of modularizing it, but Struts offers another way to modularize the struts.xml file. You could split the file into multiple xml files and import them in the following fashion.
<?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="my-struts1.xml"/>
<include file="my-struts2.xml"/>
</struts>
我们尚未涉及的另一个配置文件是 struts-default.xml。此文件包含 Struts 的标准配置设置,你无需在 99.99% 的项目中接触这些设置。因此,我们不会对这个文件进行太多详细介绍。如果你有兴趣,可以查看 struts2-core-2.2.3.jar 文件中提供的 default.properties 文件。
The other configuration file that we haven’t covered is the struts-default.xml. This file contains the standard configuration settings for Struts and you would not have to touch these settings for 99.99% of your projects. For this reason, we are not going into too much detail on this file. If you are interested, take a look into the at the default.properties file available in struts2-core-2.2.3.jar file.
The Struts-config.xml File
struts-config.xml 配置文件是 Web Client 中 View 和 Model 组件之间的链接,但你无需在 99.99% 的项目中接触这些设置。
The struts-config.xml configuration file is a link between the View and Model components in the Web Client but you would not have to touch these settings for 99.99% of your projects.
配置文件基本上包含以下主要元素 −
The configuration file basically contains following main elements −
Sr.No |
Interceptor & Description |
1 |
struts-config This is the root node of the configuration file. |
2 |
form-beans This is where you map your ActionForm subclass to a name. You use this name as an alias for your ActionForm throughout the rest of the strutsconfig.xml file, and even on your JSP pages. |
3 |
global forwards This section maps a page on your webapp to a name. You can use this name to refer to the actual page. This avoids hardcoding URLs on your web pages. |
4 |
action-mappings This is where you declare form handlers and they are also known as action mappings. |
5 |
controller This section configures Struts internals and rarely used in practical situations. |
6 |
plug-in This section tells Struts where to find your properties files, which contain prompts and error messages |
以下是样本 struts-config.xml 文件 −
Following is the sample struts-config.xml file −
<?xml version = "1.0" Encoding = "ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== Form Bean Definitions ============ -->
<form-beans>
<form-bean name = "login" type = "test.struts.LoginForm" />
</form-beans>
<!-- ========== Global Forward Definitions ========= -->
<global-forwards>
</global-forwards>
<!-- ========== Action Mapping Definitions ======== -->
<action-mappings>
<action
path = "/login"
type = "test.struts.LoginAction" >
<forward name = "valid" path = "/jsp/MainMenu.jsp" />
<forward name = "invalid" path = "/jsp/LoginView.jsp" />
</action>
</action-mappings>
<!-- ========== Controller Definitions ======== -->
<controller contentType = "text/html;charset = UTF-8"
debug = "3" maxFileSize = "1.618M" locale = "true" nocache = "true"/>
</struts-config>
有关 struts-config.xml 文件的更多详细信息,请查看你的 Struts 文档。
For more detail on struts-config.xml file, kindly check your struts documentation.
The Struts.properties File
此配置文件提供了一种机制来更改框架的默认行为。实际上, struts.properties 配置文件中包含的所有属性也可以在 web.xml 中使用 init-param 配置,以及使用 struts.xml 配置文件中的常量标记。但是,如果你希望让内容保持分离并且更具体,则可以在 WEB-INF/classes 文件夹下创建此文件。
This configuration file provides a mechanism to change the default behavior of the framework. Actually, all the properties contained within the struts.properties configuration file can also be configured in the web.xml using the init-param, as well using the constant tag in the struts.xml configuration file. But, if you like to keep the things separate and more struts specific, then you can create this file under the folder WEB-INF/classes.
此文件中配置的值将覆盖 default.properties 中配置的默认值,该值包含在 struts2-core-x.y.z.jar 发行版中。有一些属性你可以考虑使用 struts.properties 文件进行更改 −
The values configured in this file will override the default values configured in default.properties which is contained in the struts2-core-x.y.z.jar distribution. There are a couple of properties that you might consider changing using the struts.properties file −
### When set to true, Struts will act much more friendly for developers
struts.devMode = true
### Enables reloading of internationalization files
struts.i18n.reload = true
### Enables reloading of XML configuration files
struts.configuration.xml.reload = true
### Sets the port that the server is run on
struts.url.http.port = 8080
在 hash (#) 开头的任何行都会被认为是一个命令,并会被 Struts 2 忽略。
Here any line starting with hash (#) will be assumed as a comment and it will be ignored by Struts 2.