1:可以写一个字符过滤器来解决
- package com.capinfotech.filter;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.util.*;
- public class SetCharacterEncodingFilter extends HttpServlet implements Filter {
- protected String encoding = null;
- protected FilterConfig filterConfig = null;
- protected boolean ignore = true;
- // Handle the passed-in FilterConfig
- public void init(FilterConfig filterConfig) throws ServletException {
- this.filterConfig = filterConfig;
- this.encoding = filterConfig.getInitParameter("encoding");
- String value = filterConfig.getInitParameter("ignore");
- if (value == null) {
- this.ignore = true;
- } else if (value.equalsIgnoreCase("true")) {
- this.ignore = true;
- } else if (value.equalsIgnoreCase("yes")) {
- this.ignore = true;
- } else {
- this.ignore = false;
- }
- }
- // Process the request/response pair
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- // Conditionally select and set the character encoding to be used
- if (ignore || (request.getCharacterEncoding() == null)) {
- String encoding = selectEncoding(request);
- if (encoding != null) {
- request.setCharacterEncoding(encoding);
- response.setCharacterEncoding(encoding);
- }
- }
- // Pass control on to the next filter
- chain.doFilter(request, response);
- }
- protected String selectEncoding(ServletRequest request) {
- return (this.encoding);
- }
- // Clean up resources
- public void destroy() {
- this.encoding = null;
- this.filterConfig = null;
- }
- }
在web.xml里进行配置,一定要配置在Struts2的前面
- <filter>
- <filter-name>setcharacterencodingfilter</filter-name>
- <filter-class>com.capinfotech.filter.SetCharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>ignore</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>setcharacterencodingfilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
2:修改Tomcat下面conf下的server.xml的内容
- <Connector port="8088" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443"
- useBodyEncodingForURI="true" URIEncoding="utf-8"
- />
增加useBodyEncodingForURI="true" URIEncoding="utf-8"
3:在struts.xml里或者struts.properties增加struts.i18n.encoding=UTF-8