在springframework中使用velocity是非常方便的,只需在spring配置文件中申明:
<bean id="viewresolver" class="org.springframework.web.servlet.view.velocity.velocityviewresolver">
</bean>
即可在spring mvc框架中直接返回new modelandview("velocity模板", map),但是中文一直为乱码。
为了解决中文问题,首先,考虑到国际化,将所有web页面都用utf-8编码,然后在/web-inf/velocity.properties文件中覆盖velocity的默认编码iso-8859-1:
input.encoding = utf-8
output.encoding = utf-8
最后,在spring配置文件中设置:
<bean id="viewresolver" class="org.springframework.web.servlet.view.velocity.velocityviewresolver">
<property name="contenttype"><value>text/html;charset=utf-8</value></property>
</bean>
启动web服务器,可以看到中文显示正常,输入也正常。你也可以使用gbk,但是不利于多语言移植。
附:velocity简介
velocity是apache的一个开放源代码项目,它实现了可替代jsp的view层,并且以很直观的方式来编写view。编写一个velocity view就和编写一个纯html文件没有什么区别,完全可以在dreamwaver中可视化编写,只需将数据部分用$xxx替换即可。
例如,要显示一个用户信息,model传入的是一个map,包含"username","email"和"address"三个key:
<html>
<title>user: $username</title>
<body>
<p>email: $email</p>
<p>address: $address</p>
</body>
</html>
这样你就完全不必担心嵌套的jsp标签在dreamwaver中造成的语法错误。
闽公网安备 35060202000074号