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

本文介绍了一种通过自定义过滤器解决Struts框架中中文乱码问题的方法,并提供了具体的实现代码及struts-config.xml配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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值为过滤器的类路径。

posted on 2012-09-27 19:33 moonfans 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/moonfans/archive/2012/09/27/2706195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值