1 为了简单,所有文档都用utf-8编码。
2 在xxxx-servlet.xml中配置使用velocity的view的返回编码为utf-8
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix"><value>.vm</value></property>
<property name="contentType"><value>text/html;charset=UTF-8</value></property>
</bean>
3 在xxx-servlet.xml中配置velocity引擎使用的编码
<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"><property name="resourceLoaderPath">
<value>/pc/vm/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
</props>
</property>
</bean>
有人说可以配置一个velocity.properties将input.encoding的信息配置。没试过,似乎可行。
这样基本上保证无乱码了。

本文介绍如何在Spring MVC框架中通过配置velocity模板引擎来确保页面显示的字符编码为UTF-8,避免乱码问题。主要步骤包括:1. 在servlet配置文件中设置VelocityViewResolver的contentType属性;2. 使用VelocityConfigurer指定velocity引擎输入输出的编码。
5万+

被折叠的 条评论
为什么被折叠?



