今天首次使用velocity+springmvc组合,写了个测试例子,出现了乱码问题,最终通过spring下使用velocity的中文乱码问题找到答案。
具体解决添加如下:
1.添加contentType为UTF-8
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="false"></property><!--是否缓存模板-->
<property name="order" value="10" />
<property name="suffix" value=".vm"/>
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
2.添加了input.encoding和output.encoding,开始时候没有加这个,出现了乱码,后来加上,就ok了。
<!-- velocity模板配置 -->
<bean id="velocityConfig" 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>
</props>
</property>
</bean>