Struts 2 - Interceptor reset JSON Object response

I am using Struts 2 in my web application. I am writing code to check the user session for an interceptor, but for now, net.sf.json.JSONObjectas an answer, I reset it to the response object and sets it to null for the object. Check out my code.

import net.sf.json.JSONObject;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class AuthorizationInterceptor implements Interceptor {

JSONObject response = new JSONObject();

public String intercept(ActionInvocation invocation) {
 try { 
      Map session = invocation.getInvocationContext().getSession();
        if (session.get("userId") == null) {
           response.put("errorCode", "SESSION_OUT");                
            return ActionSupport.ERROR;
        } else {
            System.out.println("Session found");
            Object action = invocation.getAction();
            return invocation.invoke();
        }
    } catch (Exception e) {
        return ActionSupport.ERROR;
    }
}

public JSONObject getResponse() {
    return response;
}

public void setResponse(JSONObject response) {
    this.response = response;
}

}

How can I get a JSON object as a response from an interceptor. Please help me solve this problem.

-1
source share
1 answer

There are several errors in your code.

  • HttpServletResponse answer , you should not specify this name in JSONObject.
  • , , JSONObject , , .
  • statusCode errorCode JSON, :

statusCode, :

<result name="error" type="json">
  <param name="statusCode">304</param>
</result>

errorCode ( - , JSON):

 <result name="error" type="json">
  <param name="errorCode">404</param>
</result>

AJAX.

+2

All Articles