Search code examples
jsptomcatspring-mvcjstlsitemesh

Spring MVC rendered JSP truncated when containing special characters


If I pass variables with extended characters to my JSP, the end of the rendered file is truncated by 2 bytes for each non-ascii character. Why is this so? Here is an example.

My Controller:

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CharTestController {

    @RequestMapping(value="/chartest.do")
    public String handleRequest(ModelMap model) throws Exception {
        char var1=9829;     // Heart Char
        String var2="™";    // TM symbol
        model.addAttribute("var1", var1);
        model.addAttribute("var2", var2);
        return "chartest";
    }
}

My JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
${var1} <c:out value="${var1}" />
${var2} <c:out value="${var2}" />
9876543210

My Output:

♥ ♥ ™ ™ 98

Regardless if I use or just print the variable, since I printed 4 chars, 8 bytes are missing from the end. Any ideas?


Solution

  • Seems to be a sitemesh problem. I was using version 2.2.1. In my web.xml I had *.do pages going through sitemesh

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    And for some reason, even though it didn't match a pattern in decorators.xml, I guess sitemesh was still parsing it.

    The solution was either:

    1. specifically exclude the file from sitemesh in it's tag or
    2. upgrade sitemesh to 2.4.1

    Hopefully this will help someone in the future.