zoukankan      html  css  js  c++  java
  • Spring MVC 结合Velocity视图出现中文乱码的解决方案

    编码问题一直是个很令人头疼的事,这几天搭了一个Spring MVC+VTL的web框架,发现中文乱码了,这里记录一种解决乱码的方案。

    开发环境为eclipse,首先,检查Window->preferences->workplace->Text File Encoding,设置为GBK

    .vm文件中加入编码约束,举例如下

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=GBK">
    <title>show param</title>
    </head>
    <body>
    <h1>自动绑定的数据</h1>
    Url参数:${urlParam} </br>
    form参数:${formParam} </br>
    form文件:${formFile} </br>
    <h1>手动拉取的数据</h1>
    Url参数${urlParam1} </br>
    form参数${formParam1} </br>
    form文件${formFile1} </br>
    </body>
    </html>


     

    在spring关于velocity的配置文件中加入以下配置:

    <bean id="velocityConfigurer"
    class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"
    >
    <property name="resourceLoaderPath">
    <value>WEB-INF/views/</value>
    </property>
    <property name="velocityProperties">
    <props>
    <prop key="input.encoding">GBK</prop>
    <prop key="output.encoding">GBK</prop>
    </props>

    </property>
    </bean>
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"
    >
    <property name="suffix">
    <value>.vm</value>
    </property>
    <property name="contentType">
    <value>text/html;charset=GBK</value>
    </property>
    </bean>

    后记:

    如果eclipse的默认编码设置为utf-8,那么从一开始编辑文件,不管是英文还是中文都是utf-8的,此时按照gbk的类似流程写一遍就好了。

  • 相关阅读:
    233. Number of Digit One
    232. Implement Queue using Stacks
    231. Power of Two
    230. Kth Smallest Element in a BST
    229. Majority Element II
    228. Summary Ranges
    227. Basic Calculator II
    ArcGIS 网络分析[3] 发布NAServer到ArcGIS for Server(以Server 10.4为例)
    iView的使用【小白向】
    Vuejs环境安装与工程建立【小白Windows向】
  • 原文地址:https://www.cnblogs.com/obama/p/3842595.html
Copyright © 2011-2022 走看看