现在的位置: 首页 > 综合 > 正文

Spring集成Velocity的时候出现了乱码,

2013年11月17日 ⁄ 综合 ⁄ 共 1439字 ⁄ 字号 评论关闭
Spring集成Velocity的时候出现了乱码, 看源码发现, Spring的使用的是Velocity的实现, 默认的是ISO(晕), 开始的时候, 以为简单的:

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="false" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>

这样就可以解决问题, 没想到, 还是乱码, 看了看Velocity相关的文档, 于是改了改,

<bean id="velocifyConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="contentType">text/html;charset=UTF-8</prop>
</props>
</property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="false" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>

在velocityConfig里添加了:

<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="contentType">text/html;charset=UTF-8</prop>
</props>
</property>
以为, 这下, 肯定可以了吧, 应该改的地方都改了, 高高兴兴的重启了一下tomcat, 一访问, faint仍然乱码, 这下子崩溃了, 于是开始找, 找啊找, 找啊找, 怎么也是找不到, 看了spring的源码看了velocity的源码, 怎么也想不通(一晚上都没睡好啊), 刚刚起来的时候, 没办法, UTF-8改成了GBK, ok, 不乱了, 不过, 变成了GBK, 总是感觉不爽, 不管, 反正是不乱了.
如果Velocity乱码问题。
文件也要设置成utf-8的 , 就没问题了 

抱歉!评论已关闭.