龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

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

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
在Springframework中使用Velocity是非常方便的,只需在spring配置文件中申明: bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver" /bean 即可在spring mvc框架中直接返回

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


  
精彩图集

赞助商链接