<global-results>
<result name="exception">/WEB-INF/vm/error.vm</result>
<result name="error">/WEB-INF/vm/error.vm</result>
<result name="login" type="redirect">${ting.login.url}</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
<package name="admin" extends="erp-base">
<interceptors>
<interceptor name="HrmPrivilegeInterceptor" class="action.common.interceptor.HrmPrivilegeInterceptor"/>
<interceptor-stack name="SecurityInterceptor">
<interceptor-ref name="strutsDefaultStack"/>
<!--权限拦截 -->
<interceptor-ref name="HrmPrivilegeInterceptor"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="SecurityInterceptor"/>
</package>
项目中采用Struts2+Extjs(混杂的有Jquery)的架构进行开发,前后台之间的数据传递全部是采用JSON格式,如果前台的某一次action调用出现了session为空或者权限不够的情况时,这是需要进行页面跳转,如果是采用ajax的话,前台只是获取到跳转页面的html的代码,这样前台直接报系统异常,并不能实现真正的跳转,这时需要在前端页面中进行JS控制,才能进行页面跳转。
struts配置全局拦截:
<global-results>
<result name="exception">/WEB-INF/vm/error.vm</result>
<result name="error">/WEB-INF/vm/error.vm</result>
<result name="login" type="redirect">${ting.login.url}</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
<package name="admin" extends="erp-base">
<interceptors>
<interceptor name="HrmPrivilegeInterceptor" class="action.common.interceptor.HrmPrivilegeInterceptor"/>
<interceptor-stack name="SecurityInterceptor">
<interceptor-ref name="strutsDefaultStack"/>
<!--权限拦截 -->
<interceptor-ref name="HrmPrivilegeInterceptor"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="SecurityInterceptor"/>
</package>
后台拦截:
异步调用Struts方法返回值为void,其余的是string if(isAjaxRequest(request)) {//如果不是异步调用
return "login";
}else{
response.sendError(403);
return "success";
}
或者调用如下方法判断是否为异步调用:
private boolean isAjaxRequest(HttpServletRequest request) {
String header = request.getHeader("X-Requested-With");
if (header != null && "XMLHttpRequest".equals(header))
return true;
else
return false;
}
前台js:
$.ajaxSetup({"error":illegal});
function illegal(XMLHttpRequest, textStatus, errorThrown)
{
if(XMLHttpRequest.status==403){
window.location = "common/nopermit.html";
}else if(XMLHttpRequest.status==500){
window.location = "common/error.html";
}else ifif(XMLHttpRequest.status==408){
window.location = "login.html";
}
}
在这里设置ajax全局“错误调用”设置,jquery获取到后台传递的error信息后,自动会进行页面的跳转。这样就可以实现ajax下的页面跳转了。
tingcommon.ajaxInvoke = function (url, datajson, type) {
jQuery.ajax({
type:type,
url:url,
data:datajson,
dataType:"json",
success:function (result) {
if (result.flag == true) {
res = true;
} else {
alert(result.message);
}
}//注意,此时不能再定义error事件,不然全局error事件将不会执行。
});
};