博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP解决向Action传参乱码问题
阅读量:4672 次
发布时间:2019-06-09

本文共 2353 字,大约阅读时间需要 7 分钟。

1:可以写一个字符过滤器来解决

 

[html]
 
  1. package com.capinfotech.filter;  
  2.   
  3. import javax.servlet.*;  
  4. import javax.servlet.http.*;  
  5. import java.io.*;  
  6. import java.util.*;  
  7.   
  8. public class SetCharacterEncodingFilter extends HttpServlet implements Filter {  
  9.   
  10.     protected String encoding = null;  
  11.     protected FilterConfig filterConfig = null;  
  12.     protected boolean ignore = true;  
  13.   
  14.     // Handle the passed-in FilterConfig  
  15.     public void init(FilterConfig filterConfig) throws ServletException {  
  16.   
  17.         this.filterConfig = filterConfig;  
  18.         this.encoding = filterConfig.getInitParameter("encoding");  
  19.         String value = filterConfig.getInitParameter("ignore");  
  20.         if (value == null) {  
  21.             this.ignore = true;  
  22.         } else if (value.equalsIgnoreCase("true")) {  
  23.             this.ignore = true;  
  24.         } else if (value.equalsIgnoreCase("yes")) {  
  25.             this.ignore = true;  
  26.         } else {  
  27.             this.ignore = false;  
  28.         }  
  29.   
  30.     }  
  31.   
  32.     // Process the request/response pair  
  33.   
  34.     public void doFilter(ServletRequest request, ServletResponse response,  
  35.             FilterChain chain) throws IOException, ServletException {  
  36.   
  37.         // Conditionally select and set the character encoding to be used  
  38.         if (ignore || (request.getCharacterEncoding() == null)) {  
  39.             String encoding = selectEncoding(request);  
  40.             if (encoding != null) {  
  41.                 request.setCharacterEncoding(encoding);  
  42.                 response.setCharacterEncoding(encoding);  
  43.             }  
  44.         }  
  45.   
  46.         // Pass control on to the next filter  
  47.         chain.doFilter(request, response);  
  48.   
  49.     }  
  50.   
  51.     protected String selectEncoding(ServletRequest request) {  
  52.         return (this.encoding);  
  53.     }  
  54.   
  55.     // Clean up resources  
  56.     public void destroy() {  
  57.         this.encoding = null;  
  58.         this.filterConfig = null;  
  59.     }  
  60.       
  61. }  

 

在web.xml里进行配置,一定要配置在Struts2的前面

[html]
 
  1. <filter>  
  2.     <filter-name>setcharacterencodingfilter</filter-name>  
  3. <filter-class>com.capinfotech.filter.SetCharacterEncodingFilter</filter-class>  
  4.     <init-param>  
  5.       <param-name>encoding</param-name>  
  6.       <param-value>UTF-8</param-value>  
  7.     </init-param>  
  8.     <init-param>  
  9.       <param-name>ignore</param-name>  
  10.       <param-value>true</param-value>  
  11.     </init-param>  
  12.  </filter>  
  13.    
  14.  <filter-mapping>  
  15.     <filter-name>setcharacterencodingfilter</filter-name>  
  16.     <url-pattern>/*</url-pattern>  
  17.  </filter-mapping>  
  18.    

 

2:修改Tomcat下面conf下的server.xml的内容

[html]
 
  1. <Connector port="8088" protocol="HTTP/1.1"   
  2.               connectionTimeout="20000"   
  3.               redirectPort="8443"   
  4.               useBodyEncodingForURI="true" URIEncoding="utf-8"  
  5.               />  

增加useBodyEncodingForURI="true" URIEncoding="utf-8"

 

3:在struts.xml里或者struts.properties增加struts.i18n.encoding=UTF-8

转载于:https://www.cnblogs.com/kinghitomi/archive/2012/01/14/2322538.html

你可能感兴趣的文章