How to change encoding in struts2 to utf-8

Hi I have a test field in which I want to put the test not in English (for example, in Russian), but in my class of action I get instead of text only ????????? . I am trying to write a simple filter that describes character conversion in struts2

but it still doesn't work. can someone help me

Update i have this enter image description here

 <s:textfield key="index.login" name="login" /> 

I want to insert a test in Russian into it, and then send it to my action. But in my action class, instead of text text, I only get ????????? . To fix this problem, I need to change charset in utf8 instead of win1251.

+7
source share
3 answers

Create a filter:

 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; public class CharacterEncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html; charset=UTF-8"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } } 

Declare it in your web.xml:

 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>your.package.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

And you are good to go. Also make sure that each JSP page contains: <%@ page contentType="text/html;charset=UTF-8" language="java" %> . If your application is running on tomcat, make sure your URIEncoding="UTF-8" attribute is added to your Connector element.

+11
source

If you need to force jsp to configure on 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> 
+4
source

(cannot comment on the answer of the previous one)

 <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> 

Good for web.xml> 2.3

I'm not sure that in 2012 it does not exist yet, but note that this element is only available for web.xml> 2.4 (this element does not exist in 2.3 http://java.sun.com/dtd/web-app_2_3. dtd ).

+1
source

All Articles