For performance reasons I recently decided to switch from using JSP views to Velocity. This has worked great so far, and the response time decreased significantly. There is however one problem, I can't set the content encoding that is returned to the browser in the response headers. This worked just fine in JSP by defining the content type withWhen using Velocity the content type is always set to text/html;charset=ISO-8859-1. Characters that are not in this set are displayed as question marks. I have tried setting output.encoding, default.contentType in velocity.properties and setting an CharacterEncodingFilter in web.xml. A VelocityViewResolver has a content type property, but I want to use the ResourceBundleViewResolver.Code:<%@page contentType="text/html" pageEncoding="UTF-8"%>
I have searched using Google and this forum and there appear to be a lot of people with this problem, but I was unable to find an answer to my problem anywhere. Below are the relevant source files of a small sample application I am using to find a solution to this problem.
web.xml:
dispatcher-servlet.xml:Code:<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app>
views.properties:Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="WEB-INF/views/"/> <property name="configLocation" value="WEB-INF/classes/velocity.properties"/> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views"/> </bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /**/home.htm=HomeController </value> </property> </bean> <bean id="HomeController" class="test.HomeController"> </bean> </beans>
velocity.properties:Code:home.class=org.springframework.web.servlet.view.velocity.VelocityView home.url=home.vm home.encoding=UTF-8
home.vm:Code:output.encoding=UTF-8 input.encoding=UTF-8 default.contentType=UTF-8
HomeController.javaCode:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> Home </title> </head> <body> Chinese characters: 网生活服 </body> </html>
Thanks a lot for helping.Code:package test; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class HomeController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { return new ModelAndView("home"); } }


Reply With Quote
