拦截器拦截 ajax代码,以及乱码处理

前端之家收集整理的这篇文章主要介绍了拦截器拦截 ajax代码,以及乱码处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天又学得了如何用拦截拦截ajax,原来是异步的原因,所以要用不同的处理方法。之前用普通的拦截器,出现的现象就是跟没用拦截器时的一样。

首先要明白:

普通请求与ajax请求的报文头不一样,通过如下

  1. StringrequestType=request.getHeader("X-Requested-With");

  2. 如果requestType能拿到值,并且值为XMLHttpRequest,表示客户端的请求为异步请求,那自然是ajax请求了,反之如果为null,则是普通的请求

以下是验证登陆的拦截器,代码如下:

package com.bossin.vote.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class VoteInterceptor implements Interceptor {
private static final long serialVersionUID = 1L;

public void destroy() {
}

public void init() {
}

public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest req = ServletActionContext.getRequest();

if(req.getSession().getAttribute("login_account")==null){
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter pw = response.getWriter();
String flag = "";

if (req.getHeader("X-Requested-With") != null
&& req.getHeader("X-Requested-With").equalsIgnoreCase(
"XMLHttpRequest")) {
response.setCharacterEncoding("text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
flag = "请先登录";
pw.write(flag);
return null;
}

}
return invocation.invoke();

}
}



struts.xml 配置跟普通的拦截器就是少了返回页面的设置,具体 配置如下:

<package name="vote" extends="struts-default">
<!-- 拦截器 -->
<interceptors>
<!-- 登录验证 -->
<interceptor name="VoteInterceptor" class="com.bossin.vote.interceptor.VoteInterceptor"></interceptor>
<interceptor-stack name="verify">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="VoteInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="verify"></default-interceptor-ref>

<action name="vote" class="com.bossin.vote.action.VoteAction" method="execute"> </action>
</package>


按上面的代码是可以实现功能的,但会出现前台弹出的窗口出现中文乱码。

个人做以下处理便解决中文乱码:即把编码设置放在PrintWriter pw = response.getWriter();前面

package com.bossin.vote.interceptor; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class VoteInterceptor implements Interceptor { private static final long serialVersionUID = 1L; public void destroy() { } public void init() { } public String intercept(ActionInvocation invocation) throws Exception { HttpServletRequest req = ServletActionContext.getRequest(); if(req.getSession().getAttribute("login_account")==null){ HttpServletResponse response = ServletActionContext.getResponse(); if (req.getHeader("X-Requested-With") != null && req.getHeader("X-Requested-With").equalsIgnoreCase( "XMLHttpRequest")) { response.setCharacterEncoding("text/html;charset=utf-8"); response.setContentType("text/html;charset=utf-8"); //这两句得放在pw前,否则出现乱码 PrintWriter pw = response.getWriter(); String flag = "请先登录"; pw.write(flag); return null; } } return invocation.invoke(); } }

猜你在找的Ajax相关文章