您的位置:首页 > 理论基础 > 计算机网络

Struts 通过拦截器实现登录后跳转到登录前页面 处理普通Http请求和Ajax请求时拦截配置

2015-04-17 16:12 1396 查看
<pre name="code" class="java"><pre name="code" class="java">	//登录
public void login() throws ViewException, IOException{
String message=userManager.loginByUser(userName, pwd, null);
//		request.setAttribute("prePage", prePage);
//获取跳转到登陆界面之前的页面地址,由拦截器提供
prePage = (String) session.get("prePage");
//清除session中的数据
session.remove("prePage");
if (prePage == null) {
//不是拦截器跳转到登陆页面的,直接访问的登陆页面
prePage="index";
}
session.put("prePage", prePage);
response.getWriter().print(message);
}


//登录public void login() throws ViewException, IOException{String message=userManager.loginByUser(userName, pwd, null);// request.setAttribute("prePage", prePage); //获取跳转到登陆界面之前的页面地址,由拦截器提供 prePage = (String) session.get("prePage"); //清除session中的数据 session.remove("prePage");
if (prePage == null) { //不是拦截器跳转到登陆页面的,直接访问的登陆页面 prePage="index"; } session.put("prePage", prePage);response.getWriter().print(message);}



<pre name="code" class="html">
<interceptor name="userInterceptor" class="com.lyk.mooc.common.interceptor.UserLoginInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="createSession"></interceptor-ref>
<interceptor-ref name="parameterFilter">
<param name="blocked">token,struts.token.name,struts.token</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>


<global-results >
<result name="input" type="json"><param name="root">returnResult</param></result>
</global-results >
<action name="learnAction" class="learnAction" method="{1}">
<interceptor-ref name="userInterceptor"></interceptor-ref>
<interceptor-ref name="myStack"></interceptor-ref>
</action>

//异步请求,拦截的返回值(所有的json请求返回后执行的方法)
$(document).ajaxComplete(function(event,xhr,settings){
if(xhr.responseText=='{"PERMISSION":"NO"}'){   //验证是否为登陆状态
window.location.href ="userAction!toLogin";  //跳转页面
}
});




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐