歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> Springframwork集成Velocity的解決方案

Springframwork集成Velocity的解決方案

日期:2017/2/27 9:35:01   编辑:更多Linux
  在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中造成的語法錯誤。




Copyright © Linux教程網 All Rights Reserved