0

I have a function which popup a modal window:

function showTable(employees) {
    var result = $('#empl');
    result.empty();
    $.each(employees, function (index, employee) {
        var tr = $('<tr/>');
        tr.append($('<td/>').html(employee.id));
        tr.append($('<td/>').html(employee.name));
        result.append(tr);
    });
    $('#myModal').modal('show');
}

And code which invoke this function:

<button type="button" class="btn" onclick="showTable(${company.employees})">View</button>

My Company class

public class Company implements Serializable {
    private Integer id;
    private String name;
    private List<Employee> employees;

But function doesn't work. How to make it work?

3
  • Did you check the naming? Your JSP EL uses employee while your code states employees. Commented Apr 17, 2014 at 13:27
  • Yes. I updated it. The result is the same. Commented Apr 17, 2014 at 13:27
  • You realize that by the time the page renders, the EL has already been processed (server-side), right? onclick="showTable(${company.employees})" is NOT doing what you hope it does. Commented Apr 17, 2014 at 13:40

1 Answer 1

3

Well, you need to convert your list of employees to JSON and pass the JSON string to the JavaScript function.

In order to do that have a look at the various JSON libraries out there, e.g. Gson, Jackson, json-simple etc.

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

4 Comments

Than I can't use JSTL because of getters/setters. I can't wrap my POJO to json.
@Arkasha you must understand that all you can do here is create a JavaScript snapshot of your "Employee" object list. The Java/JSTL code runs on the server and the JavaScript runs on the client.
I can't understand how to wrap List to GSON. In my case I can't use getters and setters. So now the question is how to pass String to JSP (JSTL) without using set/getters? Is it possible?
@Arkasha you'd write a "pseudo-getter" that returns a JSON string, e.g. String getEmployeeListJSON() and use it like showTable(${company.employeeListJSON}).

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.