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的类似流程写一遍就好了。

  • 相关阅读:
    RDD的基本命令
    python 数据类型
    RDD基础
    sql优化
    python文件操作
    Python之xlsx文件与csv文件相互转换
    ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling
    python
    python操作dataFrame
    python 列表,元祖,字典
  • 原文地址:https://www.cnblogs.com/obama/p/3842595.html
Copyright © 2011-2022 走看看