spring mvc velocity中文問題

   昨天開始用spring MVC的velocity作前臺頁面開發,寫了簡單例子,在velocity的模板裏寫了中文字符,結果出現亂碼,從java後臺出入的中文沒有。html

   未解決以前的代碼:以下java

  
  
  
  
  1. <bean id="viewResolver" 
  2.         class="com.talkweb.framework.web.springmvc.view.MultipleViewResolver"> 
  3.         <property name="resolvers"> 
  4.             <map> 
  5.                 <entry key="jsp"> 
  6.                     <bean 
  7.                         class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
  8.                         <property name="viewClass" 
  9.                             value="org.springframework.web.servlet.view.JstlView" /> 
  10.                         <property name="prefix"> 
  11.                             <value>/WEB-INF/jsp/</value> 
  12.                         </property> 
  13.                     </bean> 
  14.                 </entry> 
  15.                 <entry key="vm"> 
  16.                     <bean 
  17.                         class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 
  18.                         <property name="contentType" 
  19.                             value="text/html;charset=UTF-8" /> 
  20.                     </bean> 
  21.                 </entry> 
  22.                 <entry key="ftl"> 
  23.                     <bean 
  24.                         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
  25.                         <property name="contentType" 
  26.                             value="text/html;charset=UTF-8" /> 
  27.                     </bean> 
  28.                 </entry> 
  29.             </map> 
  30.         </property> 
  31.     </bean> 
  32.  
  33. <bean id="velocityConfigurer" 
  34.         class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 
  35.         <property name="resourceLoaderPath"> 
  36.             <value>/WEB-INF/velocity/</value> 
  37.         </property> 
  38.          
  39.     </bean> 


解決以後以下:web

   
   
   
   
  1. <bean id="viewResolver" 
  2.         class="com.talkweb.framework.web.springmvc.view.MultipleViewResolver"> 
  3.         <property name="resolvers"> 
  4.             <map> 
  5.                 <entry key="jsp"> 
  6.                     <bean 
  7.                         class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
  8.                         <property name="viewClass" 
  9.                             value="org.springframework.web.servlet.view.JstlView" /> 
  10.                         <property name="prefix"> 
  11.                             <value>/WEB-INF/jsp/</value> 
  12.                         </property> 
  13.                     </bean> 
  14.                 </entry> 
  15.                 <entry key="vm"> 
  16.                     <bean 
  17.                         class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 
  18.                         <property name="contentType" 
  19.                             value="text/html;charset=UTF-8" /> 
  20.                     </bean> 
  21.                 </entry> 
  22.                 <entry key="ftl"> 
  23.                     <bean 
  24.                         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
  25.                         <property name="contentType" 
  26.                             value="text/html;charset=UTF-8" /> 
  27.                     </bean> 
  28.                 </entry> 
  29.             </map> 
  30.         </property> 
  31.     </bean> 
  32.  
  33.     <bean id="velocityConfigurer" 
  34.         class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 
  35.         <property name="resourceLoaderPath"> 
  36.             <value>/WEB-INF/velocity/</value> 
  37.         </property> 
  38.         <property name="velocityProperties"> 
  39.       <props> 
  40.        <prop key="input.encoding">UTF-8</prop> 
  41.        <prop key="output.encoding">UTF-8</prop> 
  42.        <prop key="contentType">text/html;charset=UTF-8</prop> 
  43.       </props> 
  44.      </property> 
  45.     </bean> 

先後有以下不一樣:spring

   
   
   
   
  1. <property name="velocityProperties"> 
  2.       <props> 
  3.        <prop key="input.encoding">UTF-8</prop> 
  4.        <prop key="output.encoding">UTF-8</prop> 
  5.        <prop key="contentType">text/html;charset=UTF-8</prop> 
  6.       </props> 
  7.      </property> 


若是你遇到中文問題,也一塊兒來來分享解決辦法吧!mvc

相關文章
相關標籤/搜索