In the interceptor, set the response status to UnAuthorized just before we return the errorpage.
httpResponse.setStatus(HttpStatus.SC_UNAUTHORIZED); return "errorPage";
In the ajax call where we make a request that ends up in session timeout and redirect, put a error handling block and redirect to the page directly here.
$.ajax({ url : "some.action", data : { "query" : inputText }, type : 'GET', dataType : 'json', success : function(data) { //code }, error : function(data) { if (data.status == 401 ){ console.log("UnAuthorized. Redirecting to Error"); window.location.replace(contextPath +"/error.jsp"); } }
Have a struts entry for the errorpage to avoid any config issues.
<result name="errorPage">errorPage.jsp</result>
Struts 2 Interceptor redirection not working with Ajax
سؤال
I have configured Struts 2 Interceptor for validating the user session. On invalid session I am redirecting to an error page.
The Interceptor is working fine for the normal calls to the action classes but the error page redirection is not working when I am calling an action class through ajax call.
What I am currently doing for this is sestting a request attribute in the interceptor and on the JSP based on that attribute value redirecting from the JSP.
But instead of what I am doing is there any way by which I dont need to write any thing in JSP for error page redirection to work in the similar way when calling the action class in a normal way and in the form of Ajax
Thanks, Vinay
المحلول
لا تنتمي إلى StackOverflow