概述
转载自:http://lib.csdn.net/snippet/javaee/44538
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拦截器解决请求乱码和输出乱码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复