昨天開始用spring MVC的velocity作前臺頁面開發,寫了簡單例子,在velocity的模板裏寫了中文字符,結果出現亂碼,從java後臺出入的中文沒有。html
未解決以前的代碼:以下java
- <bean id="viewResolver"
- class="com.talkweb.framework.web.springmvc.view.MultipleViewResolver">
- <property name="resolvers">
- <map>
- <entry key="jsp">
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix">
- <value>/WEB-INF/jsp/</value>
- </property>
- </bean>
- </entry>
- <entry key="vm">
- <bean
- class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
- <property name="contentType"
- value="text/html;charset=UTF-8" />
- </bean>
- </entry>
- <entry key="ftl">
- <bean
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="contentType"
- value="text/html;charset=UTF-8" />
- </bean>
- </entry>
- </map>
- </property>
- </bean>
- <bean id="velocityConfigurer"
- class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
- <property name="resourceLoaderPath">
- <value>/WEB-INF/velocity/</value>
- </property>
- </bean>
解決以後以下:web
- <bean id="viewResolver"
- class="com.talkweb.framework.web.springmvc.view.MultipleViewResolver">
- <property name="resolvers">
- <map>
- <entry key="jsp">
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix">
- <value>/WEB-INF/jsp/</value>
- </property>
- </bean>
- </entry>
- <entry key="vm">
- <bean
- class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
- <property name="contentType"
- value="text/html;charset=UTF-8" />
- </bean>
- </entry>
- <entry key="ftl">
- <bean
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="contentType"
- value="text/html;charset=UTF-8" />
- </bean>
- </entry>
- </map>
- </property>
- </bean>
- <bean id="velocityConfigurer"
- class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
- <property name="resourceLoaderPath">
- <value>/WEB-INF/velocity/</value>
- </property>
- <property name="velocityProperties">
- <props>
- <prop key="input.encoding">UTF-8</prop>
- <prop key="output.encoding">UTF-8</prop>
- <prop key="contentType">text/html;charset=UTF-8</prop>
- </props>
- </property>
- </bean>
先後有以下不一樣:spring
- <property name="velocityProperties">
- <props>
- <prop key="input.encoding">UTF-8</prop>
- <prop key="output.encoding">UTF-8</prop>
- <prop key="contentType">text/html;charset=UTF-8</prop>
- </props>
- </property>
若是你遇到中文問題,也一塊兒來來分享解決辦法吧!mvc