web.xml文件
正如前面所讨论的,这个文件为每个web应用程序提供接入点。在部署描述符(web.xml)中,Struts2 应用程序的接入点将会定义为一个过滤器。因此我们将在web.xml里定义一个FilterDispatcher类的接入点,而这个web.xml文件需要在WebContent/WEB-INF文件夹下创建。
如果你开始时没有模板或工具(比如Eclipse或Maven2)的辅助来生成,那这就是第一个你需要配置的文件。下面是我们在上一个例子中用到的web.xml的内容。
<?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>
注意,我们将Struts2 过滤器映射到 /* ,而不是 /*.action ,这意味着所有的url都会被Struts过滤器解析。当我们学到关于注解的章节时会对这个进行讨论。
注意:自2.1.3版本开始,ActionContextCleanUp和FilterDispatcher都由StrutsPrepareAndExecuteFilter代替。
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> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="cn.w3cschool.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>标签允许配置的分离和模块化。这在你进行一个大的项目并且项目分为多个不同的模块时,是非常有用的。
如果您的项目有三个域:business_applicaiton、customer_application和staff_application的话,你可以创建三个包,并将相关的Actions存储到相应的包中。 <package>标签具有以下属性:
属性 | 描述 |
---|---|
name(必需) | 为package的唯一标识 |
extends | 指定package继承另一package的所有配置。通常情况下,我们使用struts-default作为package的基础。 |
abstract | 定义package为抽象的。如果标记为true,则package不能被最终用户使用。 |
namespace | Actions的唯一命名空间 |
<constant>标签以及name和value属性将用于覆盖default.properties中定义的任一属性,就像我们设置的struts.devMode属性一样。设置struts.devMode属性允许我们在日志文件中查看更多的调试消息。
我们定义<action>标签对应于我们想要访问的每个URL,并且使用execute()方法定义一个访问相应的URL时将要访问的类。
Results(结果)确定在执行操作后返回到浏览器的内容,而从操作返回的字符串应该是结果的名称。 Results按上述方式配置,或作为“全局”结果配置,可用于包中的每个操作。 Results有
name和
type属性可选,默认的name值是“success”。
Struts.xml文件可以随着时间的推移而增长,因此通过包打破它是使它模块化的一种方式,但struts提供了另一种模块化struts.xml文件的方法,你可以将文件拆分为多个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>
我们没有覆盖的另一个配置文件是struts-default.xml。此文件包含Struts的标准配置设置,你不必再为99.99%的项目重复这些设置。 因此,我们不会深入了解这个文件的太多细节。如果你有兴趣,可以查看在struts2-core-2.2.3.jar文件中可用的default.properties文件。
struts-config.xml文件
struts-config.xml配置文件是Web Client中View和Model组件之间的链接,但在你99.99%的项目里你不必使用这些设置。 struts-config.xml配置文件包含以下主要元素:
序号 | 拦截器和说明 |
---|---|
1 | struts-config 这是配置文件的根节点。 |
2 | form-beans 这是你将ActionForm子类映射到name的位置,你可以在struts-config.xml文件的其余部分,甚至在JSP页面上,将这个name用作ActionForm的别名。 |
3 | global forwards 此部分将你在webapp上的页面映射到name,你可以使用这个name来引用实际页面。这避免了对你网页上的URL进行硬编码。 |
4 | action-mappings 这是你声明表单处理程序的地方,也被称为操作映射(action mappings)。 |
5 | controller 这部分是配置Struts的内部,在实际情况中很少使用。 |
6 | plug-in 这部分告诉Struts在哪里找到属性文件,它包含提示和错误消息。 |
下面是struts-config.xml文件的示例:
<?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 Documentation。
struts.properties文件
这个配置文件提供了一种机制来改变框架的默认行为。实际上,struts.properties配置文件中包含的所有属性也可以在web.xml中配置使用init-param,以及在struts.xml配置文件中使用constant标签。 但如果你想保持事件独立以及保留更多struts细节,那么你可以在WEB-INF/classes文件夹下创建这个文件。
struts.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
这里任何以#(hash)开头的行都将被假定为注释,并且它会被Struts 2默认忽略。
常量配置:
通常struts2加载struts2常量的顺序如下:
- struts-default.xml:该文件保存在struts2-core-2.0.6.jar文件中。
- struts-plugin.xml:该文件保存在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中。
- struts.xml:该文件是Web应用默认的Struts2配置文件。
- struts.properties:该文件是Web应用默认的Struts2配置文件。
- web.xml:该文件是Web应用的配置文件。 如果在多个文件中配置了同一个Struts2常量,则后一个文件中的配置的常量值会覆盖前面文件中配置的常量值。
在不同文件中配置常量的方式是不一样的,但不管哪个文件中,配置Struts2常量都要指定两个属性:常量name和常量value。
推荐在struts.xml文件中配置Struts2常量。
struts.xml
<struts>
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!--
该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。
-->
<constant name="struts.action.extension" value="do" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!-- spring 托管 -->
<constant name="struts.objectFactory" value="spring" />
<!--
指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration
开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。
-->
<constant name="struts.configuration"
value="org.apache.struts2.config.DefaultConfiguration" />
<!-- 设置默认的locale和字符编码 -->
<constant name="struts.locale" value="zh_CN" />
<constant name="struts.i18n.encoding" value="GBK" />
<!-- 指定Struts的工厂类 -->
<constant name="struts.objectFactory" value="spring"></constant>
<!--
指定spring框架的装配模式,装配方式有: name, type, auto, and constructor (name
是默认装配模式)
-->
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<!-- 该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true -->
<cosntant name="struts.objectFactory.spring.useClassCache" />
<!-- 指定类型检查,包含tiger和notiger -->
<cosntant name="struts.objectTypeDeterminer" value="tiger" />
<!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 -->
<constant name="struts.multipart.parser" value="cos" />
<constant name="struts.multipart.parser" value="pell" />
<constant name="struts.multipart.parser" value="jakarta" />
<!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir -->
<constant name="struts.multipart.saveDir" value="/tmpuploadfiles" />
<!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 -->
<constant name="struts.multipart.maxSize" value="2097152" />
<!--
该属性指定Struts2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖
struts.properties文件中指定的属性。如果需要加载多个自定义属性文件,多个自定义属性文
件的文件名以英文逗号(,)隔开。(也就是说不要改写struts.properties!)
-->
<constant name="struts.custom.properties"
value="application,org/apache/struts2/extension/custom" />
<!-- 指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper -->
<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />
<!-- 指定action的后缀,默认为action -->
<constant name="struts.action.extension" value="do" />
<!-- 被 FilterDispatcher使用指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true. -->
<constant name="struts.serve.static.browserCache" value="true" />
<!-- 设置是否支持动态方法调用,true为支持,false不支持. -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true. -->
<constant name="struts.enable.SlashesInActionNames" value="true" />
<!-- 是否允许使用表达式语法,默认为true. -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 设置当struts.xml文件改动时,是否重新加载 -->
<cosntant name="struts.configuration.xml.reload" value="true" />
<!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->
<cosntant name="struts.devMode" value="true" />
<!-- 设置是否每次请求,都重新加载资源文件,默认值为false. -->
<cosntant name="struts.i18n.reload" value="false" />
<!-- 标准的UI主题,默认的UI主题为xhtml,可以为simple,xhtml或ajax -->
<cosntant name="struts.ui.theme" value="xhtml" />
<!-- 模板目录 -->
<cosntant name="struts.ui.templateDir" value="template" />
<!-- 设置模板类型. 可以为 ftl, vm, or jsp -->
<cosntant name="struts.ui.templateSuffix" value="ftl" />
<!-- 定位velocity.properties 文件. 默认velocity.properties -->
<cosntant name="struts.velocity.configfile" value="velocity.properties" />
<!-- 设置velocity的context. -->
<cosntant name="struts.velocity.contexts" value="...." />
<!-- 定位toolbox -->
<cosntant name="struts.velocity.toolboxlocation" value="...." />
<!-- 指定web应用的端口 -->
<cosntant name="struts.url.http.port" value="80" />
<!-- 指定加密端口 -->
<cosntant name="struts.url.https.port" value="443" />
<!-- 设置生成url时,是否包含参数.值可以为: none,get or all -->
<cosntant name="struts.url.includeParams" value="get" />
<!-- 设置要加载的国际化资源文件,以逗号分隔. -->
<cosntant name="struts.custom.i18n.resources" value="application" />
<!-- 对于一些web应用服务器不能处理HttpServletRequest.getParameterMap(),
像 WebLogic,Orion, and OC4J等,须设置成true,默认为false. -->
<cosntant name="struts.dispatcher.parametersWorkaround" value="false" />
<!-- 指定freemarker管理器 -->
<cosntant name="struts.freemarker.manager.classname" value="org.apache.struts2.views.freemarker.FreemarkerManager" />
<!-- 设置是否对freemarker的模板设置缓存,效果相当于把template拷贝到 WEB_APP/templates. -->
<cosntant name="struts.freemarker.templatesCache" value="false" />
<!-- 通常不需要修改此属性. -->
<cosntant name="struts.freemarker.wrapper.altMap" value="true" />
<!-- 指定xslt result是否使用样式表缓存.开发阶段设为true,发布阶段设为false. -->
<cosntant name="struts.xslt.nocache" value="false" />
<!-- 设置struts自动加载的文件列表. -->
<cosntant name="struts.configuration.files" value="struts-default.xml,struts-plugin.xml,struts.xml" />
<!-- 设定是否一直在最后一个slash之前的任何位置选定namespace. -->
<cosntant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
</struts>
大部分摘抄:https://www.w3cschool.cn/struts_2/struts_configuration.html
https://blog.csdn.net/u010735684/article/details/50779918