struts2_关键的配置文件

环境:IntelliJ IDEA 2018.1.4 x64

包括了web.xmlstruts.xmlstruts-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

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章