日期:2014-05-16  浏览次数:20417 次

Struts2中使用getJSON方式进行异步调用后的页面跳转

?? 关于在struts2中使用getJSON方式进行异步调用后,拦截器返回的问题:

?

?? 项目中采用Struts2+Extjs(混杂的有Jquery)的架构进行开发,前后台之间的数据传递全部是采用JSON格式,如果前台的某一次action调用出现了session为空或者权限不够的情况时,这是需要进行页面跳转,如果是采用ajax的话,前台只是获取到跳转页面的html的代码,并不能实现真正的跳转,这时需要在前端页面中进行JS控制,才能进行页面跳转:

?

?? 首先在struts.xml文件中定义好全局的视图和拦截器,如:

?? <interceptors>
??????????? <interceptor name ="privilege" class ="cn.util.AuthorizationInterceptor" />
??????????? <interceptor-stack name="myinterceptor">
??????????? <interceptor-ref name="privilege"></interceptor-ref>
??????????? <interceptor-ref name="defaultStack"></interceptor-ref>
??????????? </interceptor-stack>
???? </interceptors>
????
?? <global-results>??
????????????? <result name="login">/login.html</result>
????????????? <result name="error">/common/error.html</result>
????????????? <result name="nopermit">/common/nopermit.html</result>??
??? </global-results>??

?

?? 然后在拦截器中判断用户访问的类型:
?/**
? * 判断是否是ajax访问
? * @param request
? * @return
? */
?private boolean isAjaxRequest(HttpServletRequest request) {
??String header = request.getHeader("X-Requested-With");
??if (header != null && "XMLHttpRequest".equals(header))
???return true;
??????? else
???return false;
?}

?

package cn.util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

?

public class AuthorizationInterceptor extends AbstractInterceptor {

?private static final long serialVersionUID = 1L;

? ?/**
? * 如果是传统访问,直接返回视图给前端页面进行跳转
? * 如果是ajax访问,系统将返回对应的错误状态码交由前端ajax的全局设置函数进行跳转
? * 错误码403表示操作未授权,500表示后台java服务端出现内部错误,408表示请求超时,session清空,自动跳转到登陆页面。
? * @WYQ
? */

@SuppressWarnings("unchecked")
?@Override
?public String intercept(ActionInvocation ai) throws Exception {
??
?? boolean flag? = false,ajaxflag = false;
?? ActionContext ctx = ai.getInvocationContext();
?? String actionName = ctx.getName();????
?? HttpServletRequest request = ServletActionContext.getRequest();
?? HttpServletResponse response = ServletActionContext.getResponse();

???

???//判断是否是ajax访问
?? ajaxflag = this.isAjaxRequest(request);
?? Map? map= ctx.getApplication();
?? Map mp = ctx.getSession();
?????
????? //拦截session为空的操作
????? if(mp == null){
???????? if(!ajaxflag){
???????????? return "login";
??????????? }else{

?? ???? ???? response.sendError(408);

???????????? return?? "login";
??????????? }
????? }
??
?????? Integer type = (Integer)? mp.get( "type" );
?????? HashMap<String,String> m = (HashMap<String,String>) map.get("actionprivilege");
?????? if(null!=m){
??????? String exclude_rolestr = m.get(actionName);
??????? if(null!=exclude_rolestr){
???????? //拦截没有权限的操作
???? ??? if(exclude_rolestr.indexOf(type+"")!=-1){
????????? flag = true;
????????? System.out.println("拦截器验证:"+actionName+".do,用户无功能的操作权限,已被系统拦截。。");
???????????? if(!ajaxflag){
????????????????return "nopermit";
???????????? }else{

??? ???? ????? ?response.sendError(403);
????????????? return?? "nopermit";
???????????? }
???????? }
??????? }
?????? }
?????????? return ai.invoke();??????
?}

?

??? 前端页面的处理结果(这里是关键)、需要导入jquery的Js库文件:
?

??$.ajaxSetup({"error":myfunc});
?
?function myfunc(XMLHttpRequest, textStatus, errorThrown)
?{
???? if(XMLHttpRequest.status==403){
? ?window.location = "common/nopermit.html";
?