如何将 JSON 响应从 struts 2 操作 class 传递到 HTML 页面

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

我正在研究 struts 2,响应 JSON
下面是我的代码

行动CLASS

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)时,表单数据被捕获到动作class(JSONDataAction) 我在浏览器中看到 json 响应,如下所示

{lastName":"User", firstName: "Test"}
我有以下疑惑

  1. 为什么浏览器不显示details.html(我只看到json 浏览器响应)。
  2. 请求属性 - json 响应中不存在 temp。如何 在 json 响应中传递请求属性。
  3. 如何处理 details.html 中的 json 响应。
  4. 如何将 JSON 响应传递给不同的视图 (HTML5) 基于 从操作返回的结果类型 class.

首先我建议你阅读the official JSON plugin documentation and this教程

Why details.html is not displayed on the browser(I see only json response on the browser).

因为您 return <result name="success" type="json"> 所以 Struts2 将 return 只有您决定 return 以 JSON 格式编辑的参数。

Request attribute - temp is not present in the json response. How to pass request attribute in json response.

正如我之前所写,JSON 响应由您的操作 class 的所有变量组成,其中包含 getter 和 setter

How to process json response in details.html.

如果您需要将一些数据处理到 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>