SpringMVC項目配置文件


配置web.xml

CharacterEncodingFilter(配置字符集過濾器,解決中文編碼問題)

  <filter>
        <filter-name>encodingFiltter</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>encodingFiltter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

DispatcherServlet(配置Spring的Servlet分發器處理所有HTTP的請求與響應)

<servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc*.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

spring-mvc.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: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://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <description>Spring MVC Config</description>

    <!--加載配置屬性文件-->
    <context:property-placeholder ignore-unresolvable="true" location="myshop.properties"/>
    <!--使用annotation自動掛載bean,只掃描@Contrller-->
    <context:component-scan base-package="com.zck.my.shop" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--默認注解映射支持-->
    <mvc:annotation-driven/>
    <!--定義視圖文件解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="${web.view.prefix}"/>
        <property name="suffix" value="${web.view.suffix}"/>
    </bean>
    <!--配置靜態資源映射-->
    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>
myshop.propertie
#view files save path
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM