站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Spring 专题Springframwork中集成Velocity的中文解决方案

Springframwork中集成Velocity的中文解决方案

摘要: Springframwork中集成Velocity的中文解决方案 在Springframework中使用Velocity是非常方便的,只需在spring配置文件中申明: <bean id='vi...
Springframwork中集成Velocity的中文解决方案
 

在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中造成的语法错误。





↑返回目录
前一篇: 快速上手Spring--8. 集合对象注入
后一篇: 解析XML的时候完全忽略DTD