struts彻底解决中文乱码问题的过滤器

package filter;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.RequestProcessor;

public class CharFilter extends RequestProcessor {

 @Override
 protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
  try {
   request.setCharacterEncoding("gbk");
   response.setCharacterEncoding("gbk");
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return true;
  
  
 }
 

}

在struts-config.xml中的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans>
   <form-bean name="UserForm" type="com.inspur.view.UserForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
   <action path="/login" name="UserForm" type="com.inspur.controll.UserAction" input="/login.jsp" validate="true">
    <forward name="success" path="/success.jsp"></forward>
    <forward name="fail" path="/error.jsp"></forward>
   </action>
   <action path="/regist" name="UserForm" type="com.inspur.controll.RegistAction" input="/regist.jsp" validate="true">
    <forward name="registSuccess" path="/registSuccess.jsp"></forward>
    <forward name="registFail" path="/registFail.jsp"></forward>
   </action>
   
  </action-mappings>
 
  <controller processorClass="filter.CharFilter">
  </controller>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

 主要添加的元素师<controller>添加的属性是processorClass值为过滤器的类路径。

原文地址:https://www.cnblogs.com/moonfans/p/2706195.html