`
碧血剑
  • 浏览: 212772 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts中文乱码解决方法

阅读更多

转载:http://blog.csdn.net/caoyinghui1986/archive/2008/03/25/2216198.aspx

初学Struts的人一定遇到过中文的乱码问题 ,目前解决的方法有很多。我这有两种经典的方法和大家分享。

1,重写 RequestProcessor   这是最简单的方法。

 public class MyRequestProcessor extends RequestProcessor {

 @Override
 protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
  try {
   request.setCharacterEncoding("UTF-8");  //在这加一行设置编码的的代吗。
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return super.processPreprocess(request, response);
 }
 
}

页面的编码也要保持一致

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

另外在struts-config.xml 中配置 <controller> 元素

<controller processorClass="com.cao.struts.MyRequestProcessor"/>

不过该方法只对 post 请求有作用

如果对get 方法也有作用processPreprocess 方法写成

if( request.getMethod().compareToIgnoreCase("post")>=0){
   try {
    request.setCharacterEncoding("UTF-8");
   } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }else{
   
   
   Iterator iter=request.getParameterMap().values().iterator();
   while(iter.hasNext())
   {
    String[] parames=(String[])iter.next();
    for (int i = 0; i < parames.length; i++) {
     try {
      parames[i]=new String(parames[i].getBytes("iso8859-1"),"UTF-8");
     } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
     }
    }   
   }   
  }

2,通过过滤器使用请求代理

public class EncodingFilter extends javax.servlet.http.HttpServlet implements Filter {
 
 FilterConfig filterConfig;
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
  // TODO Auto-generated method stub
  MyRequest req = new MyRequest( (HttpServletRequest)request);//设置代理请求
  filterChain.doFilter(req, response);

  
 }

 public void init(FilterConfig arg0) throws ServletException {
  this.filterConfig = arg0;
  
 }
}
class MyRequest extends javax.servlet.http.HttpServletRequestWrapper{//自定义的代理请求类
 HttpServletRequest request;
 public MyRequest(HttpServletRequest request) {
  super(request);
  this.request = request;
 }
 public String[] getParameterValues(String name){
  String strs[] = super.getParameterValues(name);
  for(int i=0;i<strs.length;i++){
   strs[i]  = this.myEncoding(strs[i]);
  }
  return strs;
 }
 private String myEncoding(String input){
  String output ="";
  try {
   output = new String(input.getBytes("ISO-8859-1"),"UTF-8"); //注意同页面的编码保持一致
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return output;
 }
}

在web.xml中进行配置

 <filter>
  <filter-name>encoding</filter-name>
  <filter-class>com.cao.struts.EncodingFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

该方法既可以用于post请求又可以用于get请求。

希望对大家有所帮助!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics