拦截器拦截 ajax代码,以及乱码处理
来源:互联网 发布:政府集中采购流程 知乎 编辑:程序博客网 时间:2024/06/09 22:14
今天又学得了如何用拦截器拦截ajax,原来是异步的原因,所以要用不同的处理方法。之前用普通的拦截器,出现的现象就是跟没用拦截器时的一样。
首先要明白:
普通请求与ajax请求的报文头不一样,通过如下
String requestType = request.getHeader("X-Requested-With");
如果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代码,以及乱码处理
- struts2的拦截器拦截ajax请求
- 拦截器处理类
- springmvc拦截器处理
- Struts2 拦截器处理普通Http请求和Ajax请求时拦截配置
- Struts2 拦截器处理普通Http请求和Ajax请求时拦截配置
- Struts2 拦截器处理普通Http请求和Ajax请求时拦截配置
- shiro 拦截器实现session过期拦截ajax请求的处理
- springMVC拦截器处理ajax请求及数据返回
- springMVC拦截器处理ajax请求及数据返回
- springmvc拦截器面对ajax时的处理
- servlet拦截器代码
- springmvc 异常处理、文件上传以及拦截器相关知识
- servlet中文乱码拦截器
- struts拦截器拦截ajax异步调用失败问题
- struts拦截器拦截ajax异步调用异常问题 (二)
- 实现Spring拦截器拦截ajax请求并重定向
- retrofit2拦截器处理401
- 正则表达式简单用法
- C++学习之数组名a与&a的区别
- Android:动态注册BroadcastReceiver
- hibernate的抓取策略
- 【C语言】用可变参数列表实现printf函数
- 拦截器拦截 ajax代码,以及乱码处理
- HDU 2514--Another Eight Puzzle【DFS】
- Maven仓库管理-Nexus
- android:dataPath解析
- Mark
- Mark
- JavaScript的事件代理(赞)
- 求两日期之间的相隔天数(year,month,day)
- Zipper记忆化搜索