Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: cannot set the Clinet browser to UTF-8

  1. #1
    Join Date
    Sep 2004
    Posts
    6

    Default 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. #2
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

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

    Regards,
    Darren Davison.
    Public Key: 0xE855B3EA

  3. #3
    Join Date
    Sep 2004
    Posts
    6

    Default

    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!

  4. #4
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    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,
    Darren Davison.
    Public Key: 0xE855B3EA

  5. #5
    Join Date
    Sep 2004
    Posts
    6

    Default

    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!

  6. #6
    Join Date
    Sep 2004
    Posts
    6

    Default

    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>

  7. #7
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

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

    Regards,
    Darren Davison.
    Public Key: 0xE855B3EA

  8. #8
    Join Date
    Jan 2007
    Posts
    6

    Default

    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

  9. #9
    Join Date
    Jan 2007
    Posts
    6

    Angry 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

  10. #10
    Join Date
    Mar 2007
    Location
    vancouver, canada
    Posts
    1

    Default

    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.

Similar Threads

  1. Browser Back Button Issue
    By sklakken in forum Web
    Replies: 10
    Last Post: Oct 5th, 2010, 07:47 PM
  2. JasperReports - Browser crashes
    By jkookie in forum Web
    Replies: 1
    Last Post: Sep 19th, 2005, 10:57 AM
  3. Replies: 8
    Last Post: Mar 24th, 2005, 03:49 AM
  4. Replies: 1
    Last Post: Mar 2nd, 2005, 05:48 AM
  5. Replies: 1
    Last Post: Feb 4th, 2005, 12:32 PM

Posting Permissions

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