Spring和SpringMVC环境配置

Spring SpringMVC + jackson + freemarker 依赖如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled8</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<!--freemarker-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<!--这里的版本要与spring-webmvc的版本一致-->
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<!-- jackson和目标对象交互-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- jackson注解支持-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
</project>
web.xml 配置 DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--applicationContext.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--applicationContext开头将会自动加载-->
<param-value>classpath:applicationContext*.xml</param-value>
</init-param>
<!--在web启动时自动创建spring ioc容器,并初始化dispatcherServlet-->
<load-on-startup>0</load-on-startup>
</servlet>
<!--映射-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/代表拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--只对POST请求生效 -->
<filter>
<filter-name>characterFilter</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>
</filter>
<filter-mapping>
<filter-name>characterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
将POST请求字符集改为UTF-8字符集过滤器 CharacterEncodingFilter详解
GET请求中文乱码
配置 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="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">
<context:component-scan base-package="top.xiongmingcai.restful"/>
<!--启用spring mvc注解开发模式-->
<mvc:annotation-driven>
<!--解决响应体中文乱码问题-->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--将图片/js/css等静态资源排除在外,提高执行效率-->
<mvc:default-servlet-handler/>
<!--启动Freemarker模板引擎-->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<!--设置相应输出,并解决中文乱码,客户端返回响应时,响应体所使用的字符集编码,是模板与数据渲染完成后-->
<property name="contentType" value="text/html;charset=utf-8"/>
<!--指定Freemarker模板文件拓展名-->
<property name="suffix" value=".ftl"/>
</bean>
<!--配置Freemarker参数-->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!--设置保存模板的目录-->
<property name="templateLoaderPath" value="/WEB-INF/freemarker"/>
<!--其他模板引擎设置-->
<property name="freemarkerSettings">
<props>
<!--设置Freemarker脚本与数据渲染时使用的字符集,模板与数据渲染的过程中-->
<prop key="defaultEncoding">UTF-8</prop>
</props>
</property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<!--拦截需要拦截的URL-->
<mvc:mapping path="/**"/>
<!--静态资源不做拦截-->
<mvc:exclude-mapping path="/assets/**"/>
<mvc:exclude-mapping path="/login*"/>
<!--指明那个类对拦截的URL处理-->
<bean class="top.xiongmingcai.restful.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
Spring与Spring MVC环境配置 · MingCaiXiong/spring-learn@71e86f9