spring mvcプロジェクト関連配置ファイルのまとめ

18818 ワード

web.xmlファイルの主な構成は以下の通りです.
ロードするプロファイルが必要です.クラスパスでワイルドカードの設定ができます. 類似:classipath:conf/spring/*.xml、
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
             classpath:conf/spring/spring-da.xml,
             classpath:conf/spring/spring-res.xml,
        </param-value>
    </context-param>
コンテキスト読み込みフィルタ:
<!--           context-param -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
excelテンプレートをダウンロードして、圧縮パッケージが発生しないようにします.
<!-- excel     -->
    <mime-mapping>
        <extension>xlsx</extension>
        <mime-type>application/vnd.openxmlformats</mime-type>
    </mime-mapping>
文字フィルタ:フロントバックグラウンドの中国語の文字化けを防止します.もちろん、フロントページutf-8のバックグラウンドのコードもutf-8であることを保証したほうがいいです.
<!--       -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>
SPring mvcの総制御配布構成:
<!-- Spring MVC      DispatcherServlet -->
    <servlet>
        <servlet-name>pmp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:conf/spring/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>pmp</servlet-name>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
コンテナ検証登録制御、および要求のブロック:
<!-- security start -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>pmp-web-in</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>pmp-web-in</web-resource-name>
            <url-pattern>/RES/*</url-pattern>
            <url-pattern>/login.jsp</url-pattern>
            <url-pattern>/home/error.htm</url-pattern>       
        </web-resource-collection>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/login.jsp</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <description>framework user role</description>
        <role-name>*</role-name>
    </security-role>
    <!-- security end -->
ページおよびエラージャンプの設定を歓迎します.
<welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/home/error.htm?errorCode=500</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/home/error.htm?errorCode=500</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/home/error.htm?errorCode=404</location>
    </error-page>
    <error-page>
        <error-code>403</error-code>
        <location>/home/error.htm?errorCode=403</location>
    </error-page>
以上はプロジェクトのweb.xmlの基本的な構成情報であり、自身のプロジェクト情報に基づいて設定項目を追加、削除することができます.
以下はspring-servlet.xmlファイルの関連する構成です.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
    xmlns:p="http://cxf.apache.org/policy" xmlns:ss="http://www.springframework.org/schema/security"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
       http://cxf.apache.org/policy http://cxf.apache.org/schemas/policy.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
       http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd 
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd 
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--        -->
    <context:component-scan base-package="com.lilin.pmp.web" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!--   spring mvc        -->
    <mvc:annotation-driven />
    
    <!--         -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**/*.htm"/>
            <ref bean="loginUserInterceptor" />  
        </mvc:interceptor>
    </mvc:interceptors> 
    <!--===================== view resovler ===================== -->
    <bean id="viewResolver" abstract="true">
        <property name="attributes">
            <props>
                <prop key="resRoot">@{resRoot}</prop>
                <prop key="uaaResRoot">@{uaaResRoot}</prop>
                <prop key="envName">@{envName}</prop>
                <prop key="minSuffix">@{minSuffix}</prop>
                <prop key="appVersion">@{appVersion}</prop>
                <prop key="imgHost">@{imgHost}</prop>
                <prop key="imgHostTag">@{imgHostTag}</prop>
                <prop key="imgHostNumber">@{imgHostNumber}</prop>
            </props>
        </property>
    </bean>
    <bean id="jstlViewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver"
        parent="viewResolver">
        <property name="order" value="2" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="freemarkerResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
        parent="viewResolver">
        <!--<property name="cache" value="true"/> -->
        <property name="order" value="1" />
        <property name="viewNames">
            <array>
                <value>*.ftl</value>
            </array>
        </property>
        <!-- <property name="suffix" value=".ftl" /> -->
        <property name="requestContextAttribute" value="request" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="allowSessionOverride" value="true" />
        <property name="contentType" value="text/html;charset=utf-8" /><!--   -->
        <property name="viewClass"
            value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
    </bean>
    <!-- ===================== view resolver end ====================== -->

    <!--        -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize">
            <value>104857600</value><!--          100M,100*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>

    <bean id="webPropertyConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:conf/main-setting-web.properties</value>
            </list>
        </property>
        <property name="placeholderPrefix" value="@{" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    </bean>

    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <description>Required for Freemarker to work in web tier</description>
        <property name="configuration" ref="freemarkerConfiguration" />
    </bean>

    <bean id="freemarkerConfiguration"
        class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
        <description>Using the Config directly so we can use it outside the
            web tier
        </description>
        <!--        -->
        <property name="templateLoaderPaths">
            <list>
                <value>/WEB-INF/freemarker/</value>
                <value>/WEB-INF/uaa-freemarker/</value>
                <value>classpath:/</value>
                <value>/</value>
            </list>
        </property>
        <property name="configLocation">
            <value>classpath:conf/freemarker.properties</value>
        </property>
        <!--       -->
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape" />
                <entry key="html_escape" value-ref="fmHtmlEscape" />
                <entry key="base" value="@{base}" />
                <entry key="resRoot" value="@{resRoot}" />
                <entry key="uaaResRoot" value="@{uaaResRoot}" />
                <entry key="appVersion" value="@{appVersion}" />
            </map>
        </property>
        <property name="defaultEncoding" value="utf-8" />
    </bean>

    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
    <bean id="fmHtmlEscape" class="freemarker.template.utility.HtmlEscape" />

    <!-- ====================== i18n =============================== -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:i18n/messages" />
    </bean>

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
</beans>