struts tomcat 中文乱码解决

大城市里の小女人 提交于 2019-12-16 22:46:26
 
再也不用

strRet = new String(src.getBytes("ISO_8859_1"),"UTF-8");

配置下边

 web.xml


<?xml version="1.0" encoding="Shift_JIS"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd"
>

<web-app>

     
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>htcommoninfo.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>Action Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 

  
<servlet>
    
<servlet-name>action</servlet-name>
    
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    
<init-param>
      
<param-name>config</param-name>
      
<param-value>/WEB-INF/struts-config.xml</param-value>
    
</init-param>
    
<init-param>
      
<param-name>debug</param-name>
      
<param-value>2</param-value>
    
</init-param>
    
<init-param>
      
<param-name>detail</param-name>
      
<param-value>2</param-value>
    
</init-param>
      
<init-param>  
   
<param-name>javaEncoding</param-name>  
   
<param-value>GBK</param-value>  
   
</init-param> 
<load-on-startup>2</load-on-startup>
     
</servlet>

  
<servlet-mapping>
    
<servlet-name>action</servlet-name>
    
<url-pattern>*.do</url-pattern>
  
</servlet-mapping>

  
<taglib>
    
<taglib-uri>/tags/struts-bean</taglib-uri>
    
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  
</taglib>

  
<taglib>
    
<taglib-uri>/tags/struts-html</taglib-uri>
    
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  
</taglib>

  
<taglib>
    
<taglib-uri>/tags/struts-logic</taglib-uri>
    
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  
</taglib>

  
<taglib>
    
<taglib-uri>/tags/struts-nested</taglib-uri>
    
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  
</taglib>

  
<taglib>
    
<taglib-uri>/tags/struts-tiles</taglib-uri>
    
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  
</taglib>

  
<!--
  <taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
    <taglib-location>/WEB-INF/c.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
    <taglib-location>/WEB-INF/fmt.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
    <taglib-location>/WEB-INF/sql.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
    <taglib-location>/WEB-INF/x.tld</taglib-location>
  </taglib>
  
-->

</web-app>

 
package htcommoninfo;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException; 


public class SetCharacterEncodingFilter implements Filter {

    
protected FilterConfig filterConfig;
    
protected String encodingName;
    
protected boolean enable;

    
public SetCharacterEncodingFilter() {
    
this.encodingName = "UTF-8";
    
this.enable = false;
    }


    
public void init(FilterConfig filterConfig) throws ServletException {
    
this.filterConfig = filterConfig;
    loadConfigParams();
    }


    
private void loadConfigParams() {
//    encoding
    this.encodingName = this.filterConfig.getInitParameter("encoding");
//    filter enable flag
    String strIgnoreFlag = this.filterConfig.getInitParameter("enable");
    
if (strIgnoreFlag.equalsIgnoreCase("true")) {
    
this.enable = true;
    }
 else {
    
this.enable = false;
    }

    }


    
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) 
throws IOException, ServletException {
    
if(this.enable) {
    request.setCharacterEncoding(
this.encodingName);
    }

    chain.doFilter(request, response);
    }


    
public void destroy() {
    }


    }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!