Announcement Announcement Module
Collapse
No announcement yet.
Encoding in Errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Encoding in Errors

    Hi, we have run into a problem concerning the encoding used in errors.rejectValue()
    We have a website that uses swedish characters and the errormessages is not display as they should.
    Example: errors.rejectValue("firstName", "firstNameToShort", "Du mste ange ett frnamn");

    This says that the firstName is mandatory. But when this errormessage is displayed the characters and is replaced by some bogus signs.

    Is there some configuration that needs to be done to have spring encode the text correct?

    We have the necessary locales installed on our server. And if we just set values on the request that contains swedish characters and put them on the page they are displayed correctly. So it seems like there is some spring-related issue

    Thanks for your help!

  • #2
    I'm using central european characters in my apps without problem. I don't know how could spring cause something like this. It smells more like somewhere your HTML output had been encoded wrong...

    What view technology are you using? JSPs? What charset is defined in your JSPs?
    Could be that your HttpServletResponse character encoding is invalid?

    Originally posted by Knecke View Post
    And if we just set values on the request that contains swedish characters and put them on the page they are displayed correctly.
    You put a String in request scope and then just display it on JSP, and everything is ok?

    How are the messages from message bundle displayed?

    Regards,
    Igor.

    Comment


    • #3
      Hi

      Originally posted by imilina View Post
      What view technology are you using? JSPs? What charset is defined in your JSPs?
      Yes, we are using jsp, but it is also contained in a portlet. But the charset output by the theme we use is UTF-8

      Originally posted by imilina View Post
      Could be that your HttpServletResponse character encoding is invalid?
      We never modify the response so where do i set this?

      Originally posted by imilina View Post
      You put a String in request scope and then just display it on JSP, and everything is ok?
      Yes, i picked up the requestcontext and put a value in requestparameters, then try to print it out on the page

      Originally posted by imilina View Post
      How are the messages from message bundle displayed?
      We dont use the bundling, we only use the defaultmessage part of the errorhandling to containt the errormessages

      Thanks for your reply

      Comment


      • #4
        Hmmm...try using this servlet filter:
        Code:
        import java.io.IOException;
        import javax.servlet.Filter;
        import javax.servlet.FilterChain;
        import javax.servlet.FilterConfig;
        import javax.servlet.ServletException;
        import javax.servlet.ServletRequest;
        import javax.servlet.ServletResponse;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        
        public class EncodingFilter implements Filter {
        
        	public void doFilter(ServletRequest request, ServletResponse response,  FilterChain chain) throws IOException, ServletException {
        		((HttpServletRequest)request).setCharacterEncoding("utf-8");
        		((HttpServletResponse)response).setCharacterEncoding("utf-8");
        
        		chain.doFilter(request, response);
        	}
        
        	public void init(FilterConfig filterConfig) {
        	}
        
        	public void destroy() {
        	}
        }
        and in you web.xml:
        Code:
        	<filter>
        		<filter-name>encodingFilter</filter-name>
        		<filter-class>your-package.EncodingFilter</filter-class>
        	</filter>
        to see if your problem is response encoding...

        Comment


        • #5
          Hi, I am not sure this will do any good since the response already is UTF-8

          It looks more like there is something that sets the errormessage in another encoding when using rejectValue() methods?

          Comment


          • #6
            Originally posted by Knecke View Post
            Hi, I am not sure this will do any good since the response already is UTF-8
            Are you absolutely sure about this?
            Originally posted by Knecke View Post
            It looks more like there is something that sets the errormessage in another encoding when using rejectValue() methods?
            Well, I'm not sure if rejectValue() encodes your Strings any differently. Maybe you shoud do some Spring source code digging if you suspect that this is the case.


            oh, and I forgot filter mapping, if you decide to try out your response encoding...
            Code:
            	<filter-mapping>
            		<filter-name>encodingFilter</filter-name>
            		<url-pattern>/*</url-pattern>
            		<dispatcher>REQUEST</dispatcher>
            		<dispatcher>FORWARD</dispatcher>
            	</filter-mapping>

            Comment


            • #7
              Hi, i would like to try the filter though. But there aren't any setCharacterEncoding() method on the HttpServletResponse object

              Comment


              • #8
                You're probably not using servlet spec 2.4 (or newer). But you could still System.out.println your response.getCharacterEncoding() inside filter just to check it's utf-8.Sorry for not having any better ideas...

                Comment


                • #9
                  Originally posted by imilina View Post
                  You're probably not using servlet spec 2.4 (or newer). But you could still System.out.println your response.getCharacterEncoding() inside filter just to check it's utf-8.Sorry for not having any better ideas...
                  Hi, i tried to implement the Filter but is seems like it is not triggered at all

                  <filter>
                  <filter-name>encodingFilter</filter-name>
                  <filter-class>test.EncodingFilter</filter-class>
                  </filter>

                  <filter-mapping>
                  <filter-name>encodingFilter</filter-name>
                  <url-pattern>/*</url-pattern>
                  </filter-mapping>

                  Comment


                  • #10
                    I got a hold of the encoding in the request and response characterencoding by printing out the values from requestContext.getExternalContext(). By casting the ExternalContext to a PortletExternalContext and get the request and response there. And both getCharacterEncoding() returns UTF-8

                    Comment

                    Working...
                    X