how to access variables in .jsp passed from a servlet?

I have a List of Objects that i am passing from the servlet to the jsp like this:

List<LifeEvent> lifeEvents = null;
if (initialDate.equals(finalDate))
lifeEvents = ontAccess.getPhotosOfTheDay(initialDate,null);
else
lifeEvents = ontAccess.getPhotosOfTheDay(initialDate,finalDate);

request.setAttribute("lifeEvents", lifeEvents);

The LifeEvent class has the following global variables:

@Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private long id;

    @OneToOne(optional = true, cascade = {CascadeType.ALL})
    @JoinColumn
    private IDContainer idContainer;

    @ManyToOne(optional = true, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
    private Place place;

    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private List<Comment> comments;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    @JoinTable(name = "lifeevent_withFriends")
    private List<Friend> withFriends;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    @JoinTable(name = "lifeevent_likedBy")
    private List<Friend> likedBy;

    private String text;

    private boolean highlyRelevant = false;


    @ManyToOne(optional=true, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
    @JoinColumn
    private MediaObject mediaObject;


    private boolean archived=false;

And for example the Place Object has the following global variables:

@Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private long id;

    @OneToOne(optional = true, cascade = {CascadeType.ALL})
    @JoinColumn
    protected IDContainer idContainer;

    private boolean isPublic;

    private String placeName;

    @ManyToOne
    private MediaObject media;

    @ManyToOne(optional = true, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
    private Location location;

    private boolean archived=false;

In the .jsp Page i want to display information about the LifeEvent, for example i wish to access the place name inside the Place that is inside the LifeEvent class. How can i access that?

Is just ${lifeEvent.place.placeName} ?

Upvotes: 0

Views: 184

Answers (1)

Eric Jablow
Eric Jablow

Reputation: 7899

Zero is a perfectly good number. Don't initialize a List to null. Initialize it to an empty list. isPublic is not a good name for a boolean.

Since you've made lifeEvents a request attribute, you can use the JSTL c:forEach JSP tag to iterate through the list:

<table>
    <thead>
        <tr>
            <th>Place Name</th>
            <th>Public</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th colspan="2">${fn:length(lifeEvents)} Events</th>
        </tr>
    </tfoot>
    <tbody>
        <c:forEach items="${lifeEvents}" var="event">
            <tr>
                <td>${event.place.placeName}</td>
                <td>${event.place.isPublic}</td>
            </tr>
        </c:forEach>
     </tbody>
</table>

You'll need to import the JSTL core tag library, of course.

Upvotes: 1

Related Questions