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

Struts2基础——配置

2016-05-16 09:49 459 查看

Struts2的配置文件包括:

 web.xml,

 struts.xml,

 struts-config.xml,

 struts.properties等四个文件

web.xml

文件需要在 WebContent/WEB-INF 文件夹下创建。

<?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>Struts2</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.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


我们映射 Struts 2 的过滤器为 /*,

不是 /.action*,这意味着所有的 urls 将被 struts 的过滤器解析。

struts.xml 文件

struts.xml 文件包含配置信息,随着动作的开发,你将会修改这些配置信息。这个文件可以用来重写应用程序的默认设置,例如 struts.devMode = false,还有定义在属性文件中的其他设置。这个文件可以在文件夹 WEB-INF/classes 下创建。

<?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.yangjun.struts2.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>


DOCTYPE:所有的 struts 配置文件需要有正确的 doctype,正如我们的小例子所示。 是根标签的元素,在它的下面我们使用 标签声明不同的包。在这里, 允许配置的分离和模块化。当你有一个大项目,并且该项目被划分成不同的模块时,它是非常有用的。

当然也可以将文件分割成多个 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="my-struts1.xml"/>
<include file="my-struts2.xml"/>
</struts>


也就是说,如果你的项目有三个域 - business_applicaiton,customer_application 和 staff_application,你可以创建三个包,并且在适当的包中存储相关的动作。包标签具有以下的属性:

属性描述
name (required)包的唯一标识符
extends这个包是由哪个包扩展的?默认情况下,我们使用 struts-default 作为基础包
abstract如果标记为 true,对于终端用户消费来说,这个包是不可用的
namesapce动作的唯一命名空间

struts-config.xml 文件

struts-config.xml 配置文件是在 Web 客户端中视图和模型组件之间的链接,但是你就不必要为了你的项目的 99.99% 而修改这些设置。基本配置文件包含下面的主要内容:

序号拦截器 & 描述
1struts-config:它是配置文件的根节点
2form-beans:它是你把 ActionForm 子类映射到名称上的位置。你使用这个名字作为 ActionForm 的别名,贯穿 struts-config.xml 的其余部分,甚至在 JSP 页面中
3global forwards:这个部分把 web 应用的页面映射到名称上。你可以使用该名称来引用实际的页面。这个避免了在 web 页面上硬编码 URLs
4action-mappings:它是你声明表单处理程序的位置,他们也被称为动作映射
5controller:这个部分配置 Struts 内部,而且很少在实际情况中使用
6plug-in:这个部分告诉 Struts 在哪里找到包含提示和错误信息的属性文件
<?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.properties 文件

这个配置文件提供了一种改变框架的默认行为的机制。实际上,包含在 struts.properties 配置文件内的所有属性也可以在 web.xml 中使用 init-param 被配置,同样也可以在 struts.xml 配置文件中使用 constant 标签。但是如果你喜欢保持事情分离和有更多特定的 struts,你就可以在文件夹 WEB-INF/classes 下创建这个文件。

在这个文件中配置的值将重写在 default.properties 中配置的默认值,default.properties 包含在 struts2-core-x.y.z.jar 分布中。这里有几个属性,你可能会考虑使用 struts.properties 文件改变它们:

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


在这里,任何以井号(#)开始的行会被假定为注释,它将被 Struts 2 忽略。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: