0

In my project i am getting the nonenglish character from form.If a get a form parameters as @RequestParam.It display the nonenglish character.If i get the form parameter as bean it displays some unwanted character.

<form action="selva" method="get">
<input type="text" name="s" value="அன்பு" />
<input type="submit" value="fgf"/>
</form>
It displays:அன்பு
<form:form method="post" enctype="multipart/form-data" accept-charset="UTF-8" action="multipleSave" modelAttribute="multipleSave">
    <input type="text" name="userName" value="அன்பு"/>
<input type="submit" value="submit"> 
</form:form>
It prints : திலà®à®µà®¤à®¿

How to resolve this error.

Any help will be greatly appreciated!!!

Selva
  • 1,620
  • 3
  • 33
  • 63
  • Do you have `CharacterEncodingFilter` configured in your application? – Oleksii Duzhyi Jul 21 '15 at 11:42
  • no,i am not configuring CharacterEncodingFilter – Selva Jul 21 '15 at 11:46
  • Probably this is the problem. Could you please add `CharacterEncodingFilter` to your filter chain with UTF-8 encoding. Also it would be helpful to see code example for right and wrong cases – Oleksii Duzhyi Jul 21 '15 at 11:53
  • thanks @Oleksii Duzhyi after adding the filter encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true my code works fine. – Selva Jul 22 '15 at 07:30

1 Answers1

1

You are probably missing org.springframework.web.filter.CharacterEncodingFilter. You can add it either via 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>
</filter>

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

or via Spring java configuration file:

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        ....
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        FilterRegistration.Dynamic filter = servletContext.addFilter("characterEncodingFilter", characterEncodingFilter);
        filter.addMappingForUrlPatterns(null, false, "/*");
        .....
    }
}
Oleksii Duzhyi
  • 1,203
  • 3
  • 12
  • 26