struts2解决get和post方式中文乱码

来源:互联网 发布:淘宝大学课程培训 编辑:程序博客网 时间:2024/05/16 15:26
1.首先在struts.xml文件里设置<constant name="struts.i18n.encoding" value="utf-8"/>
2.添加如下拦截器。拦截所有参数带中文的请求
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest request=  (HttpServletRequest) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
          if(request.getMethod().equalsIgnoreCase("post"))
           return invocation.invoke();
  
  Map<String, Object> params=invocation.getInvocationContext().getParameters();
    Set<Entry<String, Object>> entries=params.entrySet();
      for(Iterator<Entry<String, Object>> itor=entries.iterator();itor.hasNext();)
      {
       Entry<String, Object> entry=itor.next();
         if(entry.getValue().getClass()==String[].class)
         {
           String[] strs=(String[]) entry.getValue();
           for(int i=0;i<strs.length;i++)
           {
         strs[i]=new String(strs[i].getBytes("ISO-8859-1"), "utf-8");
           }
         }
      }
return invocation.invoke();
}
0 0
原创粉丝点击