Results 1 to 7 of 7

Thread: Encoding mismatch between spring:url and CharacterEncodingFilter

  1. #1
    Join Date
    Mar 2010
    Posts
    3

    Default Encoding mismatch between spring:url and CharacterEncodingFilter

    Hello,

    I'm having a problem with german umlauts using spring:url tag and CharacterEncodingFilter. I need the latter to get my form parameters in correct encoding. But doing url encoding, the former does not use the preferred encoding (UTF-8).

    In my case I use spring:url in my jsp:
    Code:
    <spring:url value="/path/{entity}">
    	<spring:param name="entity" value="${entity.name}" />
    </spring:url>
    If entity.name contains german umlauts, they are escaped using ISO-8859 encoding. For example, the string "für" is encoded as "f%FCr". Without the CharacterEncodingFilter the value is converted back successfully, but activating the filter results in an invalid encoded string. The value should have been encoded as "f%C3%BCr" in that case. Using URIEncoding="ISO-8859-1" in server.xml has no effect.

    I am using Spring 3.0.1 with tomcat 5.5.

    Am I missing something here? Is there an easy way to fix this?

    Regards,
    Chris

  2. #2
    Join Date
    May 2005
    Posts
    288

    Default

    Which encoding do the JSPs use?
    And did you try setting URIEncoding to UTF-8?

  3. #3
    Join Date
    Mar 2010
    Posts
    3

    Default

    Hello,

    thanks for your reply. The JSPs use UTF-8 as encoding. Setting URIEncoding to UTF-8 has no effect.

    Looking into org.springframework.web.util.UriUtils encode method reveals that there is only one escape sequence written to the output stream for every special character in the input sequence. It seems that source.charAt returns ISO-8859-1 encoded character codes for german umlauts instead of UTF-8 two byte values.

    Shouldn't source.getBytes(encoding) be used to get the single bytes of the string to be escaped (which returns two bytes for umlauts) instead of source.charAt (which, as stated above, returns one byte)?

    Chris

  4. #4
    Join Date
    May 2005
    Posts
    288

    Default

    If I understand RFC 3986 correctly, only US-ASCII characters are allowed within a URI (p. 11), so how umlauts are encoded is basically undefined. Most probably they are encoded with their unicode position, so you might need to treat the url in a different way than you treat the request parameters.

    HTH

  5. #5
    Join Date
    Mar 2010
    Posts
    3

    Default

    Hi,

    the thing is: the whole escaping and encoding is done by Spring. Shouldn't Spring handle this in a consistent manner? Or am I at least able to specify separately, which encoding should be used for path variables and which for request parameters?

    To give another example:

    The controller:
    Code:
    package example;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class TestController {
        @RequestMapping("")
        public String index() {
            return "redirect:/test/für";
        }
    
        @RequestMapping("/{value}")
        public ModelAndView test(@PathVariable("value") String value) {
            ModelAndView mv = new ModelAndView("test");
            mv.addObject("value", value);
            return mv;
        }
    }
    And the JSP:
    Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <!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>JSP Page</title>
    </head>
    <body>
        <p>provided: <spring:escapeBody>${value}</spring:escapeBody></p>
        <p><a href="<spring:url value="/test/{value}">
                        <spring:param name="value" value="für" />
                    </spring:url>">test with umlaut</a></p>
        <p><a href="/test/f%C3%BCr">this is the correct link</a></p>
    </body>
    </html>
    As stated above - everything is done by Spring, but not even the redirect results in a correct display of the value in the JSP when CharacterEncodingFilter is used.

    So what can I do?

    Chris

  6. #6
    Join Date
    May 2005
    Posts
    288

    Default

    You can write your own filter:
    Instead of just setting the encoding, it would read out the path before doing so. To put the path back into the request, write a subclass of HttpServletRequestWrapper that allows you to also set the path. Put the request into the wrapper and set the path. That way the different encoding of the path is effectively hidden from the controllers.

    You can also try to get by without umlauts in URLs altogether, which is what I would do.

    HTH

  7. #7
    Join Date
    Jan 2010
    Posts
    7

    Default Entity Escaping

    Most of us are aware of how to escape characters in HTML, so that the browser doesn't process them but still then we do come across instances where characters we have provided in the content is actually treated as code by the browsers. This is especially true when you are working with XML or XHTML ( for instance, your application is generating an XML file).

    In XML there are five characters entity references ( greater than >, less than <, ampersand &,quotation " and apostrophe ' ) that need to be escaped or the browser/parser doesn't treat them as markup. In these instances you must use entity escape code rather than the characters.

    Ampersand & &amp;
    Greater Than > &gt;
    Less Than < &lt;
    Quotation " &quot;
    Apostrophe ' &apos;

    Hope this adds values

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •