<i id='ztsJR'><tr id='ztsJR'><dt id='ztsJR'><q id='ztsJR'><span id='ztsJR'><b id='ztsJR'><form id='ztsJR'><ins id='ztsJR'></ins><ul id='ztsJR'></ul><sub id='ztsJR'></sub></form><legend id='ztsJR'></legend><bdo id='ztsJR'><pre id='ztsJR'><center id='ztsJR'></center></pre></bdo></b><th id='ztsJR'></th></span></q></dt></tr></i><div id='ztsJR'><tfoot id='ztsJR'></tfoot><dl id='ztsJR'><fieldset id='ztsJR'></fieldset></dl></div>
      1. <tfoot id='ztsJR'></tfoot>
      2. <small id='ztsJR'></small><noframes id='ztsJR'>

          <bdo id='ztsJR'></bdo><ul id='ztsJR'></ul>
      3. <legend id='ztsJR'><style id='ztsJR'><dir id='ztsJR'><q id='ztsJR'></q></dir></style></legend>
      4. 如何在 Struts 2 中使用 JSP 返回 JSON 结果

        时间:2023-10-15
            <bdo id='WjpuV'></bdo><ul id='WjpuV'></ul>
            <i id='WjpuV'><tr id='WjpuV'><dt id='WjpuV'><q id='WjpuV'><span id='WjpuV'><b id='WjpuV'><form id='WjpuV'><ins id='WjpuV'></ins><ul id='WjpuV'></ul><sub id='WjpuV'></sub></form><legend id='WjpuV'></legend><bdo id='WjpuV'><pre id='WjpuV'><center id='WjpuV'></center></pre></bdo></b><th id='WjpuV'></th></span></q></dt></tr></i><div id='WjpuV'><tfoot id='WjpuV'></tfoot><dl id='WjpuV'><fieldset id='WjpuV'></fieldset></dl></div>
              1. <tfoot id='WjpuV'></tfoot>

                  <tbody id='WjpuV'></tbody>
                <legend id='WjpuV'><style id='WjpuV'><dir id='WjpuV'><q id='WjpuV'></q></dir></style></legend>

              2. <small id='WjpuV'></small><noframes id='WjpuV'>

                  本文介绍了如何在 Struts 2 中使用 JSP 返回 JSON 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道在Struts2中可以使用json插件返回一个json类型的结果.json 也可以从 stream 结果返回,如 this 回答.

                  在 Ajax 结果的 Struts2 文档页面上JSP,我发现可以使用输出 JSON 的 JSP 返回 dispatcher 类型的结果.

                  <块引用>

                  <%@ 页面导入="java.util.Iterator,java.util.List,com.esolaria.dojoex.Book,com.esolaria.dojoex.BookManager"%><%String bookIdStr = request.getParameter("bookId");int bookId = (bookIdStr == null || "".equals(bookIdStr.trim()))?0 : Integer.parseInt(bookIdStr);Book book = BookManager.getBook(bookId);如果(书!= null){out.println(book.toJSONString());System.out.println("itis:" + book.toJSONString());}%>

                  但它使用 scriptlet 将 JSON 写入外部.我知道在 JSP 中使用 scriplets 是非常不鼓励的.但是我在这个问题中找不到我的问题的答案How can I Avoid Java code in JSP files, using JSP 2?.如何使用 JSP 结果生成 JSON 对象?有没有更好的方法从 JSP 返回 JSON 对象?

                  解决方案

                  可以通过dispatcher结果返回一个JSP,然后使用<s:property/> 标记来调用将在 JSP 中返回序列化数据的操作方法.

                  您还应该为您的 JSP 表达正确的 contentType:

                  公共类 DispatcherJsonAction 扩展 ActionSupport {私人图书;@Action("dispatcherJson")@Result(name = ActionSupport.SUCCESS, location = "page.jsp")公共字符串执行(){book = loadBookSomeHow();返回成功;}公共字符串 getJsonBook(){Gson gson = 新 Gson();尝试 {返回 gson.toJson(book);} 捕捉(异常 e){返回 gson.toJson(e.getMessage());}}}

                  page.jsp:

                  <%@page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><s:property value="jsonBook"/>

                  I know that in Struts2 can be used json plugin to return a json type result. A json could also be returned from the stream result like in this answer.

                  On the Struts2 docs page for Ajax result with JSP, I've found that it's possible to return dispatcher type result with JSP that outputs a JSON.

                  <%@ page import="java.util.Iterator,
                           java.util.List,
                           com.esolaria.dojoex.Book,
                           com.esolaria.dojoex.BookManager" %>
                  <%
                      String bookIdStr = request.getParameter("bookId");
                      int bookId = (bookIdStr == null || "".equals(bookIdStr.trim())) 
                          ? 0 : Integer.parseInt(bookIdStr);
                      Book book = BookManager.getBook(bookId);
                      if (book != null) {
                          out.println(book.toJSONString());
                          System.out.println("itis: " + book.toJSONString());
                      }
                  %>
                  

                  But it's using scriptlets to write JSON to the out. I know that using scriplets in JSP is highly discouraged. But I couldn't find the answer for my problem in this question How can I avoid Java code in JSP files, using JSP 2?. How can I use JSP result to generate a JSON object? Is there a better way to return JSON object from JSP?

                  解决方案

                  You can return a JSP through the dispatcher result, then use <s:property /> tag to call an action method that will return the serialized data in the JSP.

                  You should also express the right contentType for your JSP:

                  public class DispatcherJsonAction extends ActionSupport {
                  
                      private Book book;
                  
                      @Action("dispatcherJson")
                      @Result(name = ActionSupport.SUCCESS, location = "page.jsp")        
                      public String execute(){
                          book = loadBookSomeHow();
                          return SUCCESS;
                      }
                  
                      public String getJsonBook(){
                          Gson gson = new Gson();
                          try {
                              return gson.toJson(book);
                          } catch (Exception e){
                              return gson.toJson(e.getMessage());
                          }
                      }
                  
                  }
                  

                  page.jsp:

                  <%@page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
                  <%@taglib prefix="s" uri="/struts-tags" %>
                  <s:property value="jsonBook" />
                  

                  这篇关于如何在 Struts 2 中使用 JSP 返回 JSON 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ParametersInterceptor error: Unexpected Exception catched, E 下一篇:无法在 Struts 中加载配置

                  相关文章

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

                    <tfoot id='CblG6'></tfoot>

                        <bdo id='CblG6'></bdo><ul id='CblG6'></ul>

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