Announcement Announcement Module
Collapse
No announcement yet.
cannot set the Clinet browser to UTF-8 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • cannot set the Clinet browser to UTF-8

    Dear all,

    I use spring mvc + velocity
    I have some pages in Chinese and I chose to use utf-8 as the encoding

    however, I cannot set the client browser's encoding by any of the following methods:

    1. by adding
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    in the velocity template

    2. by setting the request and response in the Controller:

    public class TestController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    return new ModelAndView("test");
    }
    }

    3. or by saving my templates in utf-8 encording.

    The client browser always uses iso-8859-1 as the encording

    Thanks!
    ahchiu

  • #2
    specify the property on your view definition; ie in views.properties (or equivalent):
    http://www.springframework.org/docs/...a.lang.String)

    Regards,

    Comment


    • #3
      Dear davison,

      I am new to spring. Could you tell me how can I set the view defiinition of velocity?
      Do I set it in my servlet context?

      the setting in my test-servlet.xml is

      <bean id="velocityConfig" class="org.springframework.web.servlet.view.veloci ty.VelocityConfigurer" singleton="true">
      <property name="resourceLoaderPath"><value>/WEB-INF/velocity/</value></property>

      <bean id="viewResolver" class="org.springframework.web.servlet.view.veloci ty.VelocityViewResolver">
      <property name="cache"><value>true</value></property>
      <property name="exposeSpringMacroHelpers"><value>true</value></property>
      <property name="prefix"><value>srp/</value></property>
      <property name="suffix"><value>.vm</value></property>
      </bean>


      Where and how should I put the content-type in the servlet context?

      Thank you very much!

      Comment


      • #4
        ah ok, if you're using VelocityViewResolver you need to set the property there rather than on the views themselves since you're not configuring views individually. This will set the content type for ALL views resolved (which may be what you want, but just to warn you if not).

        On your VelocityViewResolver definition, set the bean property as follows:
        Code:
         <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
          <property name="cache"><value>true</value></property>
          <property name="exposeSpringMacroHelpers"><value>true</value></property>
          <property name="prefix"><value>srp/</value></property>
          <property name="suffix"><value>.vm</value></property>
          <property name="contentType"><value>text/html;charset=UTF-8</value></property>
        </bean>
        Regards,

        Comment


        • #5
          Dear Davison,

          Thank you very very much!

          In addition to the content-type property, I found that I also need set the velocity configurations:
          input.encoding=utf-8
          output.encoding=utf-8

          so as to display the utf-8 Chinese characters correctly.

          Thanks again!

          Comment


          • #6
            dear davison

            where can i find the documentation on the properties of the VelocityViewResolver?


            <property name="cache"><value>true</value></property>
            <property name="exposeSpringMacroHelpers"><value>true</value></property>
            <property name="prefix"><value>srp/</value></property>
            <property name="suffix"><value>.vm</value></property>
            <property name="contentType"><value>text/html;charset=UTF-8</value></property>

            Comment


            • #7
              the API docs and reference docs can be found linked from the spring site at http://www.springframework.org/documentation.html

              Regards,

              Comment


              • #8
                Hello Darren,

                I'm working on an multi-language application and have set all properties the same way as Achiu did as described in this chain. Unfortunately, my browser still displays garbage. For example the string that populates a field in the Velocity template reads: Conexión (spanish)
                The display and the sourceView from the browser shows: Conexión which appears to be an incorrect encoding.
                My velocity.properties parameters are set correctly to:
                input.encoding=UTF-8
                output.encoding=UTF-8
                The meta tag onthe template is set correctly and the property on the View resolver definition is also set correctly to:
                <property name="contentType"><value>text/html;charset=utf-8</value></property>
                Here's the printout of the Resquest headers as read by the browser:
                -----------------
                Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
                Referer: http://localhost:7001/ccert/
                Accept-Language: en-us,es;q=0.8,nl-be;q=0.7,it;q=0.5,es-pr;q=0.3,ja;q=0.2
                Content-Type: application/x-www-form-urlencoded
                Accept-Encoding: gzip, deflate
                User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; Amgen.v1b; .NET CLR 1.1.4322)
                Host: localhost:7001
                Content-Length: 31
                Connection: Keep-Alive
                Cache-Control: no-cache
                UTF-8 UTF8
                These above are returned character encoding from request and response respectively.
                -----------------


                Please help.

                Thanks,

                Jay Latman

                Comment


                • #9
                  Browser displays garbage despite correct settings

                  Hello All,

                  I'm working on an multi-language application and have set all properties the same way as Achiu did as described in this chain. Unfortunately, my browser still displays garbage. For example the string that populates a field in the Velocity template reads: Conexión (spanish)
                  The display and the sourceView from the browser shows: Conexión which appears to be an incorrect encoding.
                  My velocity.properties parameters are set correctly to:
                  input.encoding=UTF-8
                  output.encoding=UTF-8
                  The meta tag onthe template is set correctly and the property on the View resolver definition is also set correctly to:
                  <property name="contentType"><value>text/html;charset=utf-8</value></property>
                  Here's the printout of the Resquest headers as read by the browser:
                  -----------------
                  Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
                  Referer: http://localhost:7001/ccert/
                  Accept-Language: en-us,es;q=0.8,nl-be;q=0.7,it;q=0.5,es-pr;q=0.3,ja;q=0.2
                  Content-Type: application/x-www-form-urlencoded
                  Accept-Encoding: gzip, deflate
                  User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; Amgen.v1b; .NET CLR 1.1.4322)
                  Host: localhost:7001
                  Content-Length: 31
                  Connection: Keep-Alive
                  Cache-Control: no-cache
                  UTF-8 UTF8
                  These above are returned character encoding from request and response respectively.
                  -----------------


                  Please help.

                  Thanks,

                  Jay Latman

                  Comment


                  • #10
                    Hi Jay,

                    I was wondering if you've found a solution to your problem. I'm seeing a similar issue where we have our template and properties files saved as UTF-8 and all configuration set to use UTF-8.

                    Any input would be appreciated.

                    Comment


                    • #11
                      Using UTF-8 properties files with Velocity

                      Hey kentuckycyclist,

                      I actually realized that I did not have enough time to get it working correctly but I found a way around it.

                      You still have to set property of VelocityViewResolver to UTF-8.
                      Example:

                      (<bean class="org.springframework.web.servlet.view.veloci ty.VelocityViewResolver">
                      <property name="contentType"><value>text/html;charset=utf-8</value></property>)

                      In your xx_YY.properties files that has unprintable characters, you convert these characters into the Unicode NCR character format.
                      Example:

                      actual test in spanish: Certificación del Código de Conducta
                      converted: Certificaci& # 243 ; n del C & # 243 ; digo de Conducta

                      Make sure that string that begins with & and ends with ; has no spaces in between. I had to put spaces there so this site displays them correctly.

                      That works.

                      I converted these characters using a web site:

                      http://www.pinyin.info/tools/convert...ninumbers.html

                      If you find a better way, please let me know.

                      Jay
                      Last edited by Jay Latman; Mar 9th, 2007, 06:56 PM. Reason: NCR characters were converted

                      Comment


                      • #12
                        using encodings other than iso-8859-1

                        I am new to spring but I took a look and spring just calls
                        response.getWriter() and feeds the result of the template into that.

                        This means if you don't call response.setContentType() BEFORE
                        the template is rendered the default of "text/html; charset=iso-8859-1" is
                        used.

                        Servlet containers pick up on the charset=xxx part and use
                        that as the encoding for the PrintWriter. (which does the conversion from unicode). This is a good thing since it means the encoding should match the Content-Type header.

                        If you want html in utf-8 encoding, call

                        response.setContentType("text/html; charset=utf-8");

                        somewhere before rendering your template.

                        Would be nice if there was some way to override the default
                        content type in a configuration file somewhere. Does anyone know
                        an easy way to do this ?

                        Comment


                        • #13
                          This is a Spring BUG

                          I can now confirm that this is a Spring BUG.

                          I have done everything and I mean everything. I have added the char set filter, updated the content type of my view resoilver. Added UTF-8 content type to my html header on all my files. I have followed every tip found on the internet.

                          To be sure I placed a System.out.println and a logger.debug in my validator and controller. I then fill in a value in a input field in a form that posts to a URL that is mapped to the controller and I see this in the catalina.out and my log file:

                          This costs â¬

                          The value I supplied was This costs €. To make sure it is a Spring BUG I debug my web application by setting up a debug session in Tomcat and eclipse. I put break points in the validator and controller. In both classes the value in the command bean thtis mapped to te form is This costs â¬

                          Spring is ignoring every attempt to set the content type to UTF-8.

                          This clearly means that there is a serious flaw in Spring somewhere.

                          Comment


                          • #14
                            This works OK for me so it must be a configuration problem. I do the following:

                            1) Add the CharacterEncodingFilter as the FIRST filter in web.xml:

                            Code:
                            <filter>
                            	<filter-name>CharacterEncodingFilter</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>
                            </filter>
                            
                            <filter-mapping>
                            	<filter-name>CharacterEncodingFilter</filter-name>
                            	<url-pattern>/*</url-pattern>
                            </filter-mapping>
                            2) Set the content type in the JSPs:

                            Code:
                            <%@ page contentType="text/html;charset=UTF-8" %>
                            3) Add the content type to the head section of the html (not sure if this is really needed):

                            Code:
                            <head>
                                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                            </head>
                            Hope this helps.

                            Chris

                            Comment


                            • #15
                              I have done all 3 and it still isnt working. I must say instead of seeing This costs ⬠in the log I see This costs ? so the unknown character has changed from ⬠to ? but still this is not correct.

                              Comment

                              Working...
                              X