配置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