環境:IntelliJ IDEA 2018.1.4 x64
包括了web.xml、struts.xml、struts-config.xml以及struts.properties。
web.xml(web/WEB-INF)
部署描述符,是一種J2EE配置文件,決定servlet容器的HTTP元素需求如何進行處理。它嚴格來說不是一個Struts2 配置文件,但它是Struts2 運作所需要進行配置的文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
這個文件爲每個web應用程序提供接入點。在部署描述符(web.xml)中,Struts2 應用程序的接入點將會定義爲一個過濾器。因此我們將在web.xml裏定義一個StrutsPrepareAndExecuteFilter(該過濾器類文件在struts2-core-xxx.jar org.apache.struts2.dispatcher.filter中 )類的接入點。我們將Struts2 過濾器映射到 /* ,也就是所有的url都會被Struts過濾器解析。
struts.xml(/src/)
主要用於配置action,也可以用於覆蓋應用程序的默認設置。如:
<constant>標籤以及name和value屬性將用於覆蓋default.properties(struts2-core-2.2.3.jar文件中)中定義的任一屬性。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--國際化-->
<constant name="struts.custom.i18n.resources" value="message"/>
<!--<constant name="struts.i18n.encoding" value="UTF-8" />-->
<package name="default" namespace="/" extends="struts-default">
<!--<interceptors>-->
<!--<interceptor name="timer" class="com.org.interceptor.TimerInterceptor">-->
<!--</interceptor>-->
<!--</interceptors>-->
<action name="Login" class="com.org.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="login">/index.jsp</result>
<result name="input">/error.jsp</result>
<result name="wait">/wait.jsp</result>
<result name="invalid.token">/error.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="execAndWait">
<param name="delay">1500</param>
</interceptor-ref>
<!--<interceptor-ref name="timer"></interceptor-ref>-->
</action>
<action name="check" class="com.org.action.CheckAction">
<result>/index.jsp</result>
</action>
<action name="SecurityCodeImageAction"
class="com.org.action.SecurityCodeImageAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="bufferSize">2048</param>
</result>
</action>
</package>
</struts>
struts-config.xml
struts-config.xml配置文件是Web Client中View和Model組件之間的鏈接,但在你99.99%的項目裏你不必使用這些設置。
<?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>
序號 | 攔截器和說明 |
---|---|
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在哪裏找到屬性文件,它包含提示和錯誤消息。 |
(取自 https://www.w3cschool.cn/struts_2/struts_configuration.html )
struts.properties
這個配置文件提供了一種機制來改變框架的默認行爲。實際上,struts.properties配置文件中包含的所有屬性也可以在struts.xml配置文件中使用constant標籤。 但如果你想保持事件獨立以及保留更多struts細節,那麼你可以在web/WEB-INF/classes文件夾下創建這個文件。
struts.properties文件中配置的值將覆蓋default.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