Struts 2 - Interceptor reset the response JSON Object

后端 未结 1 1211
终归单人心
终归单人心 2020-12-22 13:12

I am using Struts 2 in my web application. I write code to check user session into interceptor but while I am returning net.sf.json.JSONObject as response, its

相关标签:
1条回答
  • 2020-12-22 13:57

    There are mutliple errors in your code.

    • Response is HttpServletResponse, you should not give that name to a JSONObject.
    • Interceptors are NOT THREAD-SAFE, that means that you should define the JSONObject inside the method, to prevent multiple users to update it concurrently, one over the other
    • You should use statusCode or errorCode as described in the JSON plugin documentation, by configuring it as the Error result you are returning:

    Use statusCode to set the status of the response:

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

    And errorCode to send an error(the server might end up sending something to the client which is not the serialized JSON):

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

    and then read it client-side in the AJAX callback function.

    0 讨论(0)
提交回复
热议问题