0

I am working on struts 2 with JSON response
Below is my code

ACTION 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>  

I have added the struts2-json-plugin-2.3.24.jar to the lib folder as required

When I submit the form(employee.html), the form data is captured in action class (JSONDataAction) and I see the json response in browser as shown below

{lastName":"User", firstName: "Test"}
I have the following doubts

  1. Why details.html is not displayed on the browser(I see only json response on the browser).
  2. Request attribute - temp is not present in the json response. How to pass request attribute in json response.
  3. How to process json response in details.html.
  4. How to pass the JSON response to the different views(HTML5) based on result type returned from action class.
Karthik
  • 1,302
  • 5
  • 25
  • 56

1 Answers1

0

First of all I suggest you to read the official JSON plugin documentation and this tutorial

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

Because you return <result name="success" type="json"> so Struts2 will return only the parameters you decide to be returned in JSON format.

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

The JSON response, as I wrote before, is formed of all the variables of your action class that has a getter and setter

How to process json response in details.html.

If you need to process some data into the html page you have to use the Struts2 tags. like <s:property value="lastName" /> and <s:property value="firstName" />. There is no need at all of JSON for this kind of actions. If you wanna submit the form with an Ajax call then the JSON response can be usefull, but in this case you just need <result name="success">/pages/details.html</result>.

I also suggest you to use JSP instead of HTML pages. So the result page will be:

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>  
IlGala
  • 3,331
  • 4
  • 35
  • 49
  • 1
    Thanks for the response. My objective is not to use struts 2 tags and use html 5 and bind it with JSON response from action class as performance will be better. So I would like to know how to pass the JSON response to the different views(HTML5) based on result type returned from action class.. – Karthik Aug 19 '15 at 08:37
  • Karthik you can't return a view from an action that has a JSON response. The JSON plugin provides a "json" result type that serializes actions into JSON. The serialization process is recursive, meaning that the whole object graph, starting on the action class (base class not included) will be serialized – IlGala Aug 19 '15 at 08:43
  • 1
    Ok. Please may I know what has to done to get JSON response in view(details.html)(do I need to make an Ajax call/write Jquery function in the same view(details.html) to get the JSON from action class). – Karthik Aug 19 '15 at 08:47
  • Yes, The best way to achive your goal is to submit the form via AJAX call, create the result inside the action and represent the result. – IlGala Aug 19 '15 at 08:53
  • Thanks for clarifications. I understood in what scenarios JSON response is needed from action class(in scenarios where response is required without page refresh). It would be helpful if I can get an example for - submit the form via AJAX call, create the result inside the action and represent the result. – Karthik Aug 19 '15 at 08:58
  • No problem mate! www.google.com and search Struts2 JSON and JQuery :). [Here is an example](http://www.simplecodestuffs.com/ajax-implementation-in-struts-2-using-jquery-and-json/) of how to populate a select (the first result i've found) – IlGala Aug 19 '15 at 09:06
  • I need one more clarification. If I want my request attributes set in action class in JSON response. Should I declare them as member variables in my action class or is there any other way of passing them in JSON response. – Karthik Aug 19 '15 at 12:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87384/discussion-between-ilgala-and-karthik). – IlGala Aug 19 '15 at 12:58