簡體   English   中英

如何將Struts 2動作類的JSON響應傳遞到HTML頁面

[英]How to pass JSON response from struts 2 action class to HTML page

我正在使用Struts 2進行JSON響應
下面是我的代碼

動作課

public class JSONDataAction implements ServletRequestAware{

    private String firstName;
    private String lastName;

    protected HttpServletRequest request;

    public String execute() {

        System.out.println("FIRST NAME IN ACTION CLASS IS::"+firstName);
        System.out.println("LAST NAME IN ACTION CLASS IS::"+lastName);      

        request.setAttribute("temp", "temp data");  

        return "success";
   }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletRequest getServletRequest() {
        return request;
    }
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />

    <package name="jsonView" namespace="/" extends="struts-default,json-default">

       <action name="getJSONResult" class="com.javatechig.struts2web.actions.JSONDataAction">
           <result name="success" type="json">/pages/details.html</result>
       </action>

   </package>
</struts>

employee.html

<html>
    <body>
        <h4>
            Struts 2 HTML5 Example
        </h4>

        <form action="getJSONResult" method="post">
            Enter first name: <input type = "text" name="firstName"><br>
            Enter last name : <input type = "text" name="lastName"><br> 
            <input type="submit">
        </form>
    </body>
</html>

details.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Details</title>
</head>
<body>
      EMPLOYEE DETAILS :::  
</body>
</html>  

我已根據需要將struts2-json-plugin-2.3.24.jar添加到lib文件夾中

當我提交表單(employee.html)時,表單數據將在操作類(JSONDataAction)中捕獲,並且在瀏覽器中看到json響應,如下所示

{lastName“:“用戶”,firstName:“測試”}
我有以下疑問

  1. 為什么details.html沒有顯示在瀏覽器上(我在瀏覽器上只能看到json響應)。
  2. 請求屬性-json響應中不存在temp。 如何在json響應中傳遞請求屬性。
  3. 如何在details.html中處理json響應。
  4. 如何根據操作類返回的結果類型將JSON響應傳遞給不同的視圖(HTML5)。

首先,我建議您閱讀官方的JSON插件文檔教程

為什么details.html沒有顯示在瀏覽器上(我在瀏覽器上只能看到json響應)。

因為您返回<result name="success" type="json">所以Struts2將僅返回您決定以JSON格式返回的參數。

請求屬性-json響應中不存在temp。 如何在json響應中傳遞請求屬性。

就像我之前寫的那樣,JSON響應由具有getter和setter的操作類的所有變量組成

如何在details.html中處理json響應。

如果需要將一些數據處理到html頁面中,則必須使用Struts2標簽。 <s:property value="lastName" /><s:property value="firstName" /> 此類操作完全不需要JSON。 如果要通過Ajax調用提交表單,則JSON響應可能會有用,但是在這種情況下,您只需要<result name="success">/pages/details.html</result>

我還建議您使用JSP而不是HTML頁面。 因此結果頁面將是:

details.jsp

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Details</title>
</head>
<body>
    <s:property value="lastName" />
    <s:property value="firstName" />
</body>
</html>  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM