zoukankan      html  css  js  c++  java
  • freemarker页面中文乱码

    一、前言

    简单的记录freemarker遇到的错误问题:ftl页面中文乱码

    由于freemarker整合在ssm框架中,所以笔者直接贴配置代码

    <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">
    
         <!-- 注册spring的默认server,来出来静态资源 -->
        <mvc:default-servlet-handler/>
        <!-- 注册一些bean,只要用途就是处理请求的映射和调用 -->
        <mvc:annotation-driven/>
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
        <context:component-scan base-package="com.ys.controller" /> 
        <!-- 添加注解驱动 --> 
        <mvc:annotation-driven enable-matrix-variables="true" />
        <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
        <!-- 静态资源处理  css js imgs -->
       <!--  <mvc:resources location="/resources/**" mapping="/resources" />
        <mvc:resources location="/back_css/" mapping="back_css/**" />
        <mvc:resources location="/back_js/" mapping="/back_js/**" />
        <mvc:resources location="/back_img/" mapping="/back_img/**" />
        <mvc:resources location="/skin/" mapping="/skin/**" /> -->
            
        <!-- 告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件 -->
        <context:component-scan base-package="com.ys.controller"  use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan> 
        <!--避免IE执行AJAX时,返回JSON出现下载文件 -->  
        <bean id="mappingJacksonHttpMessageConverter"  
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
            <property name="supportedMediaTypes">  
                <list>
                    <value>text/html;charset=UTF-8</value>  
                </list>  
            </property>  
        </bean>  
        <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->  
        <bean  
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
            <property name="messageConverters">  
                <list>  
                    <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->  
                </list>  
            </property>  
        </bean>  
          
        <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->  
        <bean id="multipartResolver"    
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
            <!-- 默认编码 -->  
            <property name="defaultEncoding" value="utf-8" />    
            <!-- 文件大小最大值 -->  
            <property name="maxUploadSize" value="10485760000" />    
            <!-- 内存中的最大值 -->  
            <property name="maxInMemorySize" value="40960" />    
            <!-- 启用是为了推迟文件解析,以便捕获文件大小异常 -->
            <property name="resolveLazily" value="true"/>
        </bean>   
        
        <!-- 配置ViewResolver 。可用多个ViewResolver 。使用order属性排序。   InternalResourceViewResolver 放在最后-->
        <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"></property>
            <property name="mediaTypes">
                <map>
                    <!-- 告诉视图解析器,返回的类型为json格式 -->
                    <entry key="json" value="application/json" />
                    <entry key="xml" value="application/xml" />
                    <entry key="htm" value="text/htm" />
                </map>
            </property>
            <property name="defaultViews">
                <list>
                    <!-- ModelAndView里的数据变成JSON -->
                    <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
                </list>
            </property>
            <property name="ignoreAcceptHeader" value="true"></property>
        </bean>
         <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
       <!--JSP视图解析器-->
        <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <property name="templateLoaderPath" value="/WEB-INF/views"/>
            <!-- 设置页面中文乱码问题 -->
            <property name="freemarkerSettings"> 
            <props>
            <prop key="defaultEncoding">UTF-8</prop> 
            </props>
            </property> 
        </bean>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <!-- 解决freemarker中文乱码 -->
            <property name="contentType" value="text/html;charset=UTF-8"/>        
            <property name="cache" value="true"/>
            <property name="prefix" value=""/>
            <property name="suffix" value=".ftl"/>
            <property name="order" value="0"/>
        </bean>
        <mvc:interceptors>
            <mvc:interceptor >
                <mvc:mapping path="/**"/>
             <!--    对于静态资源,可以通过后缀名
                <mvc:exclude-mapping path="/**/*.js"/>
                <mvc:exclude-mapping path="/**/*.css"/>
                <mvc:exclude-mapping path="/**/*.jpg"/>
                <mvc:exclude-mapping path="/**/*.gif"/>
                <mvc:exclude-mapping path="/**/*.png"/>
                也可以通过文件夹, 加这些exclude-mapping就不会被拦截器拦截到,资源能够正常访问 -->
                <mvc:exclude-mapping path="/back_css/**"/>
                <mvc:exclude-mapping path="/back_js/**"/>
                <mvc:exclude-mapping path="/back_img/**"/>
                <mvc:exclude-mapping path="/skin/**"/>
                <bean class="com.ys.interceptors.DefaultInterceptors"/>
            </mvc:interceptor>
        </mvc:interceptors> 
    </beans>

    需要注意的就是:

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <property name="templateLoaderPath" value="/WEB-INF/views"/>
            <!-- 设置页面中文乱码问题 -->
            <property name="freemarkerSettings"> 
            <props>
            <prop key="defaultEncoding">UTF-8</prop> 
            </props>
            </property> 
        </bean>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <!-- 解决freemarker中文乱码 -->
            <property name="contentType" value="text/html;charset=UTF-8"/>        
            <property name="cache" value="true"/>
            <property name="prefix" value=""/>
            <property name="suffix" value=".ftl"/>
            <property name="order" value="0"/>
        </bean>

    这两个配置文件,其中org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer在spring-webmvc.jar 包里面,项目要导入这个jar包才可以,或者会找不到类。 

    注意:还有一种常见的导致乱码问题:你页面的编码跟文件保存的编码不一致时就会出现错误。右键修改为UTF-8就可以。

     

  • 相关阅读:
    Codeforces 1316B String Modification
    Codeforces 1305C Kuroni and Impossible Calculation
    Codeforces 1305B Kuroni and Simple Strings
    Codeforces 1321D Navigation System
    Codeforces 1321C Remove Adjacent
    Codeforces 1321B Journey Planning
    Operating systems Chapter 6
    Operating systems Chapter 5
    Abandoned country HDU
    Computer HDU
  • 原文地址:https://www.cnblogs.com/IT-1994/p/6216428.html
Copyright © 2011-2022 走看看