我是靠谱客的博主 香蕉棉花糖,这篇文章主要介绍struts2拦截器解决请求乱码和输出乱码,现在分享给大家,希望可以做个参考。

转载自:http://lib.csdn.net/snippet/javaee/44538

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.UnsupportedEncodingException; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.StrutsStatics; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class EncodingIntereptor extends AbstractInterceptor { private static final long serialVersionUID = 6826256332417695666L; @Override public String intercept(ActionInvocation invo) throws Exception { ActionContext actionContext = invo.getInvocationContext(); HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST); HttpServletResponse response= (HttpServletResponse) actionContext.get(StrutsStatics.HTTP_RESPONSE); if( request.getMethod().compareToIgnoreCase("post")>=0){ try { request.setCharacterEncoding("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }else{ Iterator<String[]> iterval=request.getParameterMap().values().iterator(); while(iterval.hasNext()){ String[] parames = iterval.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(); } } } } response.setContentType("text/html;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); return invo.invoke(); } }


最后

以上就是香蕉棉花糖最近收集整理的关于struts2拦截器解决请求乱码和输出乱码的全部内容,更多相关struts2拦截器解决请求乱码和输出乱码内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部