您的位置:首页 > 编程语言 > Java开发

[Struts2官方指南的个人学习和翻译] Struts2的配置文件

2015-04-19 08:35 459 查看
Configuration Files

从一个struts开发者的角度来看, 必须要关注的一个配置文件是 web.xml。 在这里,你可以控制整个struts框架和你的应用的配置. 默认的情况下, Struts 会加载一些内置的配置文件来配置自己,
然后用另外的配置文件来配置你的应用,尽管可能不需要编写除了web.xml以外的配置文件就可以开发一个struts应用。

你可以使用下列表格中的配置文件来配置你的struts应用. 有些配置文件可以被动态的加载. See Reloading configuration for more.

File
Optional
Location (relative to webapp)
Purpose
web.xml
no
/WEB-INF/
Web deployment descriptor to include all necessary framework components

包含了所有框架需要的组件的web配置文件
struts.xml
yes
/WEB-INF/classes/
Main configuration, contains result/view types, action mappings, interceptors, and so forth

主配置文件,包含了 result/view types, action mappings, interceptors等
struts.properties
yes
/WEB-INF/classes/
Framework properties

框架的参数
struts-default.xml
yes
/WEB-INF/lib/struts2-core.jar
Default configuration provided by Struts

Struts提供的默认配置文件
struts-default.vm
yes
/WEB-INF/classes/
Default macros referenced by
velocity.properties

struts-plugin.xml
yes
At the root of a plugin JAR
Optional configuration files for Plugins in the same format as
struts.xml
.

可选的插件配置文件,格式如同struts.xml
velocity.properties
yes
/WEB-INF/classes/
Override the default Velocity configuration


Static Content

框架需要一些常用的静态组件 (JavaScript ,CSS 等等) 来为FilterDispatcher 过滤器服务. Any request starting with "
/struts/
" denotes that static content is required, and then mapping the value after "
/struts/
" to common packages in the framework and, optionally
in the application's class path.

默认的, 将在下列的包中寻找:

org.apache.struts2.static
template

额外的包可以在配置文件中名为"packages"的参数列表中添加 (在 web.xml 为 FilterDispatcher 过滤器配置). 当添加额外的静态组件时, 要注意不要暴露了敏感的配置信息 (如数据库的密码).

web.xml

Web.xml配置文件是Java Web应用的核心, 所以同样也是struts框架的核心.在web.xml中, Struts 定义了自己的过滤器 FilterDispatcher,这个Servlet Filter 类用来初始化Struts框架和处理所有请求. 这个过滤器可以包含一些初始化参数,来表明哪些额外的配置文件需要被加载,框架该有哪些行为等等。

除了FilterDispatcher, Struts 还提供了一个ActionContextCleanUp 类,当其他过滤器(如那些Sitemesh所使用的)需要访问一个已经初始化的struts框架时候,用来执行特殊的清理任务。


Key Initialization Parameters

config - 一个用逗号隔开的列表表示需要被加载的xml文件

actionPackages -
一个用逗号隔开的列表表示action需要用到的java包.

configProviders -
一个用逗号隔开的列表表示实现了ConfigurationProvider 接口的java类,that should be used for building the Configuration.

loggerFactory - 实现LoggerFactory接口的类的名字.

* - 其他表示框架的常量的参数.


Simple Example

为struts配置为web.xml中关键的一点是添加一个过滤器和其映射关系。

FilterDispatcher Example (web.xml)

Changed Filter Structure in Struts >= 2.1.3
Icon

为了划分dispatcher阶段,FilterDispatcher 在Struts 2.1.3版本后就不再使用了. 如果你使用的是版本,应该这样使用:

See SiteMesh Plugin for an example on when to use seperate Filters for prepare and execution phase

Why the Filter is mapped with /* and how to configure explicit exclusions (since 2.1.7)
Icon

在上例中,我们为Struts 2 dispatcher映射为”
/*"
, 则 Struts 2 会处理所有请求. 这是因为Struts 2 使用自己的jar包来提供静态组件服务, 包括 Dojo JavaScript 文件 (如果使用S2.0, 或者 S2.1版本之后的Dojo插件) 和 FreeMarker 模块为struts2标签生成html文件。

如果将过滤器的映射改为其他如
/*.html
, we must take this in to account and extract the content that would normally be served from the Struts 2 jar files, or some other solution.

Since Struts 2.1.7, you are able to provide a comma seperated list of patterns for which when matching against the

request URL the Filter will just pass by. This is done via the configuration option struts.action.excludePattern, for example in your struts.xml


Taglib Example

通常情况下,不需要也不推荐配置一个标签库. 标签库已经被包含在
struts-core.jar中
, 容器将会自动寻找到它.


如果因为一些原因需要在web.xml中配置一个标签库, 将TLD文件从META-INF文件中的
struts-core.jar
中提取出来,
然后在web.xml文件中添加一个taglib标记


Custom FileManager and FileManagerFactory implementations

如果需要支持应用服务器中的特殊文件系统(如 JBoss中的VFS), 你使用自己的FileManager接口的实现类. 但必须在整个框架初始化前配置它。

使用 <init-param/>标记来使用自己的FileManager接口的实现类,如下:

使用 <init-param/>标记来使用自己的FileManagerFactory实现类, 如:

去看看默认的实现类 - DefaultFileManager.java 和 DefaultFileManagerFactory.java 来了解更多。

struts.xml

Struts框架的核心配置文件是默认的
struts.xml
文件,应该将它放在web项目的class路径之下 (通常为
/WEB-INF/classes
).

默认的配置文件会包含其它需要的配置文件。
struts-plugin.xml
文件可以放置在一个JAR文件中,应用会自动将其安装。所以一个模块可以是独立的,自动配置的。.

I如 Freemarker 和 Velocity 模块, 会自动从classpath中载入,所以一个模块可以独立写成一个JAR包.

Q:可以将一个struts.xml分成几个模块吗?

A:可以,有两种方法。

1.使用<include>标记来引用另外的配置文件,会按先后顺序加载。

2.使用JAR包。一个模块会被加载至应用里,通过在JAR包里放置一个struts.xml,然后将JAR包放在classpath路径下。

struts.properties


所有的properties文件都可以用XML文件中的<constant>标记来表示.

Struts使用了一些properties文件,它们可以根据你的需要修改。当修改任何的properties时, 只需要在
struts.properties文件中指定他的键和值。
properties文件 可以放在classpath里的任意地方,通常放在
/WEB-INF/classes之下。


struts-default.properties

struts-default.xml

Struts2.jar
中包含了一个基本的配置文件 struts-default.xml. 它会被自动添加进struts.xml中,来提供标准的配置设置。
Icon

To exclude the
struts-default.xml
or to provide your own version, see the
struts.configuration.files
setting in struts.properties.

struts-default.xml 的部分内容:

文件中定义了所有默认的results 和 interceptors,还有 interceptor stacks 。注意package的name为"struts-default".

struts-default.properties

struts-default.properties

Generated by CXF SiteExporter
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: