1

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?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Pravin Varpe
  • 188
  • 12
  • If you don't want to do it in JS why do you return JSON? – Aleksandr M May 05 '14 at 07:51
  • Because I want to use ajax functionality i.e I don't want to reload the page. Is there any another way that i can get a values on JSP from action without reloading the page? – Pravin Varpe May 05 '14 at 10:28
  • Return HTML from ajax call not JSON and add it to page. – Aleksandr M May 05 '14 at 10:32
  • How can we send html from Struts2 Action? Can we? – Pravin Varpe May 05 '14 at 10:37
  • You are probably doing it already... using `dispatcher` result `/WEB-INF/...`. :) – Aleksandr M May 05 '14 at 10:40
  • But using dispatcher we just redirect to the JSP page where html code is already created. In my case as you said, I need to create & return html code from within action class. How can i do that? And second thing when we use dispatcher, page will reload. Where JSON result type prevents page reload. – Pravin Varpe May 05 '14 at 10:49
  • 1
    What stops you to return `dispatcher` result from ajax called action? – Aleksandr M May 05 '14 at 10:51
  • I will check it out & get back to you. – Pravin Varpe May 05 '14 at 10:54
  • 1
    @Aleksandr Thank you very much for your superb guidance. I think i have got the point what you were trying to say. I have successfully passed the `List` to the `` tag by using `dispatcher'. Thanks allot once again. – Pravin Varpe May 05 '14 at 11:27
  • You are welcome. You can add your solution as an answer for future visitors. – Aleksandr M May 05 '14 at 11:29
  • Yes Sure. But i will first implement it perfectly in my code by checking all the possible conditions with all pros & cons. Then will post the answer. – Pravin Varpe May 05 '14 at 11:34

2 Answers2

1

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.

Pravin Varpe
  • 188
  • 12
0

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>  
kriyeta
  • 695
  • 4
  • 12