0

I have a form which submit details like name, address, affiliations. Here these inputs will in different language like french, Spanish, German, Russian and so on. I point that these inputs are some time have non English keyboard character and are submitted as different character like &,^ and so on.

for example,

this is the input

Instituto de Quı´mica, Universidade de Sa˜ o Paulo, Sa˜ o Paulo, Brazil

and this is the data that saved in DataBase while I submit the form

Instituto de Qu?´mica, Universidade de Sa˜ o Paulo, Sa˜ o Paulo, Brazil

I have set the character set as UTF-8 in database and in jsp page first later I found that struts 2 form has a tag attribute acceptcharset="UTF-8"

and it has been working for only few other language but not for Spanish, Portuguese nad many more. so what is the solution for this issue?

Sathish Kumar k k
  • 1,732
  • 5
  • 26
  • 45

3 Answers3

1

I have Fix this by changing UTF-8 in pageEncoding and charset in HTML page where ever I see this and in form i used acceptcharset="UTF-8" and last I get issue in storing it in DB even its charset is charset is UTF-8 so I forced DB connection to use UTF-8 by providing jdbc:mysql://localhost:3306/yourDB?useUnicode=true&characterEncoding=utf8 in connection url

Sathish Kumar k k
  • 1,732
  • 5
  • 26
  • 45
0

You can use Spring filter, and force the encoding to UTF8. Add this to your web.xml:

  <filter>
    <filter-name>encodingFilter</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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Atropo
  • 12,231
  • 6
  • 49
  • 62
  • do I need any spring plug in for this? – Sathish Kumar k k Jul 27 '12 at 08:43
  • you need to add the spring libraries to your project. If you don't use spring injection capabilities into the struts2 actions you don't need the struts-spring-plugin. – Atropo Jul 27 '12 at 08:46
  • @SathishKumarkk: even if you are not using Spring you can create a simple filter which will set the request encoding before passing control – Umesh Awasthi Jul 27 '12 at 08:58
  • @Umesh Awasthi Can you please tell how to do that – Sathish Kumar k k Jul 27 '12 at 09:01
  • @SathishKumarkk: Spring is open source,, have a look at source code of this filter and you can adapt that to your way.here is the link to source code http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-web/2.5.1/org/springframework/web/filter/CharacterEncodingFilter.java – Umesh Awasthi Jul 27 '12 at 09:06
  • @Umesh Awasthi as you have said that without using spring we can do this so tell me how to do it. – Sathish Kumar k k Jul 27 '12 at 09:11
  • @SathishKumarkk: i already provided the link of that filter, create your own filter and use/adapt that code, map your filter in your web.xml before struts dispatcher filter and you are all set to go – Umesh Awasthi Jul 27 '12 at 09:13
  • @Atropo: only if you using spring else there is no benefits of adding extra dependencies for a single filter – Umesh Awasthi Jul 27 '12 at 10:34
  • @Umesh Awasthi According to [this link](http://stackoverflow.com/a/6929337/896718) I think I need to create an interceptor instead of filter. Am I right? – Sathish Kumar k k Jul 27 '12 at 12:21
  • @SathishKumarkk: No create filter so that it should be application to each request being passed to the underlying system – Umesh Awasthi Jul 28 '12 at 04:29
0

If you need to force jsp to UTF-8 you can write the following in web.xml:

<jsp-config>
    <jsp-property-group > 
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>
Christian
  • 819
  • 1
  • 8
  • 23