Announcement Announcement Module
Collapse
No announcement yet.
Setting content encoding in Velocity Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting content encoding in Velocity

    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 with
    Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    When 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.

    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:
    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>
    dispatcher-servlet.xml:
    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>
    views.properties:
    Code:
    home.class=org.springframework.web.servlet.view.velocity.VelocityView
    home.url=home.vm
    home.encoding=UTF-8
    velocity.properties:
    Code:
    output.encoding=UTF-8
    input.encoding=UTF-8
    default.contentType=UTF-8
    home.vm:
    Code:
    <!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>
    HomeController.java
    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");
        }
    
    }
    Thanks a lot for helping.

  • #2
    Try to add in your dispatcher-servlet.xml, at viewResolver bean definition a contentType definition like this:

    Code:
    ...
        <property name="contentType" value="text/html; charset=UTF-8" />
    ...

    Comment


    • #3
      I know the VelocityViewResolver has a contentType property, but the ResourceBundleViewResolver does not.

      Code:
      org.springframework.beans.InvalidPropertyException: Invalid property 'contentType' of bean class [org.springframework.web.servlet.view.ResourceBundleViewResolver]: No property 'contentType' found
      I'm considering to use the VelocityViewResolver anyway, but I still prefer the ResourceBundleViewResolver because it's easier to manage in larger applications.

      Thanks anyway.


      -----EDIT-----

      I'm such an idiot.

      views.properties:
      Code:
      simple.class=org.springframework.web.servlet.view.velocity.VelocityView
      simple.url=simple.vm
      simple.contentType=text/html; charset=utf-8
      does the trick.
      Last edited by Eddy Dean; May 23rd, 2009, 03:41 AM. Reason: problem solved

      Comment


      • #4
        Good, this is a trick not to forget.

        Comment

        Working...
        X