1

I'm new to JSP & JSTL, I have a json array as below :

[ { "id" : 1, "name" : "A" }, { "id" : 2, "name" : "b" }]

which I need to print in HTML table view.

I have tried to use the code said here

<table>
    <c:forEach items="${persons}" var="person">
        <tr>
            <td>${person.name}</td>           
        </tr>
    </c:forEach>
</table>

But I am getting this error

javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String

Someone correct me where I went wrong.

6
  • 1
    Has the json been converted to a list of persons? And that list is only being used in the html page? Commented Apr 30, 2014 at 10:22
  • Show your print HTML table method. Commented Apr 30, 2014 at 10:23
  • @bigGuy this what i used. <table> <c:forEach items="${persons}" var="person"> <tr> <td>${person.name}</td> </tr> </c:forEach> </table> Commented Apr 30, 2014 at 10:23
  • 1
    @Parthi04 - How do you expect the JSTL to work then? If you provide the json string as such to the JSTL, it obviously can't find an object which has the name field/property in it. Parse your json to a list of person objects and use that in the JSTL Commented Apr 30, 2014 at 10:24
  • I bet you didnt copied this line from eexample: List<Person> persons = new Gson().fromJson(jsonPersons, new TypeToken<List<Person>>() {}.getType()); Commented Apr 30, 2014 at 10:27

1 Answer 1

2

You cant directly use that as jstl treats the value as a string. You may either need to convert it to some java object in your servlet and use it in jstl. You can do the covertion using jackson or gson or some other library.

Or if you dont want to do all that and keep it simple you can use jquery rather than jstl like

var jsonData = '[ { "id" : 1, "name" : "A" }, { "id" : 2, "name" : "b" }]';

$($.parseJSON(jsonData)).map(function () {
       return '<tr class="child"><td>' + this.name + '</td></tr>';
}).appendTo('#myTable tbody');

Where "myTable" is the id of the table.

How to do in controller

You have DetailedJson in your controller. This could be a json array or string. Then in controller you could do the following using gson, then your code should work

String detailedJson = "[ { 'id' : 1, 'name' : 'A' }, { 'id' : 2, 'name' : 'b' }]";
Gson gson = new Gson();
Type listType = new TypeToken<List<Person>>() {}.getType();
List<Person> persons = gson.fromJson(detailedJson, listType);
request.setAttribute("persons", person);

So now your request scope has a list of Person objects with id and name. Your jstl could take this an create table without any change. The person is just a pojo. for eg.

public class Person {

    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm pretty much new to jstl ans jsp.. i have a variable ${DetailedJson} which directly prints the json putput which is coming from controller... so how i can proceed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.