spring mvc velocity中文问题

   昨天开始用spring MVC的velocity做前台页面开发,写了简单例子,在velocity的模板里写了中文字符,结果出现乱码,从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> 


解决之后如下:

   
   
   
   
  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> 

前后有如下不同:

   
   
   
   
  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> 


如果你遇到中文问题,也一起来来分享解决办法吧!

 

你可能感兴趣的:(spring,mvc,velocity,职场,休闲,中文问题)