Question

I am getting JSON object via ajax call using JQuery. JSON object contains list of model (i.e Bean)

e.g List<myModel>

I am using struts2 json plugin.

JSON object as a string looks as given below.

{"myList":[{"age":26,"degree":"MCA","fname":"Pravin","id":1,"lname":"Varpe"},
{"age":26,"degree":"MCA","fname":"Pramod","id":2,"lname":"Patil"},
{"age":24,"degree":"B.E","fname":"Atul","id":3,"lname":"Vairale"},
{"age":22,"degree":"M.E","fname":"Sudeep","id":4,"lname":"Masare"},
{"age":21,"degree":"BCS","fname":"Nikhil","id":1,"lname":"Pethe"},
{"age":21,"degree":"MCS","fname":"Abhijeet","id":1,"lname":"Supekar"}]}

I want to pass this JSON object to <s:iterator> tag as given below

<s:iterator value = "myJsonObject">
     <s:property value = "fname"/>
     // properties 
</s:iterator>

I can display this JSON object on jsp by iterating it into jquery function & creating html element but I don't want to do that. So is it possible to directly pass JSON to <s:iterator> tag anyhow?

Was it helpful?

Solution

On discussion with @Aleksandr I found the Answer. I am posting it here so that any one facing same problem can get a help form it.

To <s:iterator> tag we can not pass JSON object & it's not at all needed. What I had to achieve is to make a ajax call & pass a result to the <s:iterator>.

So no need to use a Struts2 json plugin. Simply use dispatcher which is default result type of action in Struts2 which returns html. Simple steps are

  1. Create separate JSP page say List.jsp which will contain <s:iterator> tag to which we will pass the result(i.e List<myModel> in my case).

  2. Give this List.jsp as a result page of our action as show below.

    <s:action name="myAction" class="package.MyClass">
        <result name = "success">List.jsp</result>
    </s:action>
    
  3. So on making ajax call we will get List.jsp(i.e html) as a response. Simply add this response data to the any div you want.

That's it.

OTHER TIPS

First of all, you need to get "mylist" Object then you need to iterate through "myList" and then again use JsonObject iterator to get key-value pairs.

<s:iterator value="test.get('myList')" var="listJsonArray">
    <s:iterator value="listJsonArray" var="row">  
        <s:iterator value="row" var="jsonObject" > 
            <s:property/>
        </s:iterator> 
    </s:iterator> 
</s:iterator>  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top