SSM项目解决视图解析器解析html页面乱码问题

直接上解决方法:
这是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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--    扫描controller-->
    <context:component-scan base-package="com.itcode.controller"></context:component-scan>
    <mvc:annotation-driven/>
    <!--    设置本地磁盘资源文件路径-->
    <context:property-placeholder location="classpath:file.properties"/>

    <mvc:default-servlet-handler/>

    <!-- 静态资源解析
    包括 :js、css、img、..
     -->
    <mvc:resources location="assets/js/" mapping="assets/js/**"/>
    <mvc:resources location="assets/images/" mapping="assets/images/**"/>
    <mvc:resources location="assets/css/" mapping="assets/css/**"/>
    <mvc:resources location="assets/fonts/" mapping="assets/fonts/**"/>
    <!-- 定义视图文件解析 html专用-->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">1</prop>
                <!--解决乱码 start-->
                <prop key="default_encoding">UTF-8</prop>
                <!--解决乱码 end-->
                <prop key="number_format">0.##</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
            </props>
        </property>
    </bean>
    <bean id="htmlviewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix" value=".html" />
        <property name="order" value="0"></property>
        <property name="contentType" value="text/html;charset=UTF-8"></property>
    </bean>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置页面路径的前缀 -->
        <property name="prefix" value="/WEB-INF/pages/"/>
        <!-- 配置html路径的后缀 -->
        <property name="suffix" value=".html"/>
    </bean>
    <!-- 自定义参数绑定 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 转换器 -->
        <property name="converters">
            <list>
                <!-- 日期类型转换 -->
                <bean class="com.itcode.controller.converter.DateConverter"/>
            </list>
        </property>
    </bean>
    <!-- 全局异常处理器
只要实现HandlerExceptionResolver接口就是全局异常处理器
 -->
    <bean class="com.itcode.exception.ContentExceptionResolver"></bean>
    <!-- 文件上传 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为5MB -->
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize">
            <value>5242880</value>
        </property>
    </bean>
</beans>

通过在上述文件中添加如下配置,即可解决控制器解析HTML页面产生的中文乱码问题。

<!-- 定义视图文件解析 html专用-->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">1</prop>
                <!--解决乱码 start-->
                <prop key="default_encoding">UTF-8</prop>
                <!--解决乱码 end-->
                <prop key="number_format">0.##</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
            </props>
        </property>
    </bean>
    <bean id="htmlviewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix" value=".html" />
        <property name="order" value="0"></property>
        <property name="contentType" value="text/html;charset=UTF-8"></property>
    </bean>

此外,还需在web.xml文件中配置:

<!-- post乱码过虑器 -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>*.html</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>
上一篇:Oracle 恢复delete误删数据


下一篇:Node.js 获取微信JS-SDK CONFIG