views:

65

answers:

2

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?

A: 

Are you suggesting that it all works when you leave Spring/Spring-MVC out of the context?

At least, try adding the following line to the very bottom of the JSP file to nail down the root cause:

<% response.getWriter().flush(); %>

If this fixes the problem, then the problem is probably indeed in the servlet or filter of Spring MVC. If not, then the problem is probably more in the servletcontainer in question. Which one are you using?

BalusC
Thanks for the reply. Adding your suggestion to the end produces the same result. I'm running tomcat (using the SpringSource tool suite). I'm fairly new to the JSP/Spring/Tomcat world so I don't know how to test the parts independently. I'm assuming some configuration somewhere is using the wrong charset so character sizes or output buffer sizes are being computed incorrectly, I just don't know which places to check.
Jason
I can't reproduce your problem in "plain" JSP/Servlet on Tomcat 6.0.24.
BalusC
+1  A: 

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.

Jason