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

fjl0120fjl 2015-08-05 05:42:03
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();
}
...全文
184 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
scmod 2015-08-05
  • 打赏
  • 举报
回复
web容器一般有直接参数改的感觉
迷林 2015-08-05
  • 打赏
  • 举报
回复
楼主这是散分的节奏吗?

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧