• <small id='94EIH'></small><noframes id='94EIH'>

  • <tfoot id='94EIH'></tfoot>

        <i id='94EIH'><tr id='94EIH'><dt id='94EIH'><q id='94EIH'><span id='94EIH'><b id='94EIH'><form id='94EIH'><ins id='94EIH'></ins><ul id='94EIH'></ul><sub id='94EIH'></sub></form><legend id='94EIH'></legend><bdo id='94EIH'><pre id='94EIH'><center id='94EIH'></center></pre></bdo></b><th id='94EIH'></th></span></q></dt></tr></i><div id='94EIH'><tfoot id='94EIH'></tfoot><dl id='94EIH'><fieldset id='94EIH'></fieldset></dl></div>
        <legend id='94EIH'><style id='94EIH'><dir id='94EIH'><q id='94EIH'></q></dir></style></legend>
          <bdo id='94EIH'></bdo><ul id='94EIH'></ul>

        Struts 2 - 拦截器重置响应 JSON 对象

        Struts 2 - Interceptor reset the response JSON Object(Struts 2 - 拦截器重置响应 JSON 对象)

      1. <small id='hTaMy'></small><noframes id='hTaMy'>

          <tfoot id='hTaMy'></tfoot>

              • <legend id='hTaMy'><style id='hTaMy'><dir id='hTaMy'><q id='hTaMy'></q></dir></style></legend>
                  <bdo id='hTaMy'></bdo><ul id='hTaMy'></ul>
                    <tbody id='hTaMy'></tbody>
                  <i id='hTaMy'><tr id='hTaMy'><dt id='hTaMy'><q id='hTaMy'><span id='hTaMy'><b id='hTaMy'><form id='hTaMy'><ins id='hTaMy'></ins><ul id='hTaMy'></ul><sub id='hTaMy'></sub></form><legend id='hTaMy'></legend><bdo id='hTaMy'><pre id='hTaMy'><center id='hTaMy'></center></pre></bdo></b><th id='hTaMy'></th></span></q></dt></tr></i><div id='hTaMy'><tfoot id='hTaMy'></tfoot><dl id='hTaMy'><fieldset id='hTaMy'></fieldset></dl></div>
                  本文介绍了Struts 2 - 拦截器重置响应 JSON 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的 Web 应用程序中使用 Struts 2.我编写代码将用户会话检查到拦截器中,但是当我返回 net.sf.json.JSONObject 作为响应时,它会重置响应对象并将 null 设置为对象.请检查我的代码.

                  导入 net.sf.json.JSONObject;导入 com.opensymphony.xwork2.interceptor.Interceptor;公共类 AuthorizationInterceptor 实现拦截器 {JSONObject 响应 = 新 JSONObject();公共字符串拦截(ActionInvocation 调用){尝试 {映射会话 = invocation.getInvocationContext().getSession();if (session.get("userId") == null) {response.put("errorCode", "SESSION_OUT");返回 ActionSupport.ERROR;} 别的 {System.out.println("找到会话");对象动作 = invocation.getAction();返回调用.invoke();}} 捕捉(异常 e){返回 ActionSupport.ERROR;}}公共 JSONObject getResponse() {返回响应;}公共无效 setResponse(JSONObject 响应){this.response = 响应;}

                  }

                  如何获取 JSON 对象作为拦截器的响应.请帮我解决这个问题.

                  解决方案

                  您的代码中有多个错误.

                  • 响应是 HttpServletResponse,你应该不要将该名称赋予 JSONObject.
                  • 拦截器不是线程安全的,这意味着您应该在方法内定义 JSONObject,以防止多个用户同时更新它,一个在另一个之上
                  • 您应该按照 JSON 插件中的说明使用 statusCode 或 errorCode 文档,将其配置为您返回的错误结果:
                  <块引用>

                  使用 statusCode 设置响应的状态:

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

                  <块引用>

                  和 errorCode 发送错误(服务器可能最终向客户端发送不是序列化 JSON 的内容):

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

                  然后在 AJAX 回调函数中读取它的客户端.

                  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 reset the response object and set null to the object. Please check 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 the JSON Object as response from interceptor. Please help me to resolve this issue.

                  解决方案

                  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.

                  这篇关于Struts 2 - 拦截器重置响应 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                  • <bdo id='7wQY6'></bdo><ul id='7wQY6'></ul>
                    • <legend id='7wQY6'><style id='7wQY6'><dir id='7wQY6'><q id='7wQY6'></q></dir></style></legend>
                              <tbody id='7wQY6'></tbody>
                            <i id='7wQY6'><tr id='7wQY6'><dt id='7wQY6'><q id='7wQY6'><span id='7wQY6'><b id='7wQY6'><form id='7wQY6'><ins id='7wQY6'></ins><ul id='7wQY6'></ul><sub id='7wQY6'></sub></form><legend id='7wQY6'></legend><bdo id='7wQY6'><pre id='7wQY6'><center id='7wQY6'></center></pre></bdo></b><th id='7wQY6'></th></span></q></dt></tr></i><div id='7wQY6'><tfoot id='7wQY6'></tfoot><dl id='7wQY6'><fieldset id='7wQY6'></fieldset></dl></div>

                            <small id='7wQY6'></small><noframes id='7wQY6'>

                            <tfoot id='7wQY6'></tfoot>