Results 1 to 2 of 2

Thread: creating POJO

  1. #1

    Default creating POJO

    Hi

    I am new to Spring framework and a newbie in Java too. I am having an issue that I want to solve.

    In my object, which I want to display on the front end, there are few properties which have multiple values. For example, for a person, his country and state. As these properties values which can be displayed as a drop down list, I am not able to figure out how to implement them such that I can display them on the front end.

    Do I need to create another object? or, is there any other way possible.
    How do I do it?

    Please help

  2. #2
    Join Date
    Jul 2006
    Location
    Philadelphia, PA, USA
    Posts
    341

    Default

    Hi GameOne,

    Not sure if I completely understand your question, but if an object can have multiple values and you want to display them, I would think you would want some Collection object (probably either a List or a Set) reference in the object. For example, assuming that a Person can have multiple Countries:

    Code:
    public class Person {
    
       private Set<Country> countries;
    
       public Set<Country> getCountries() {
          return this.countries;
       }
    
       public void setCountries(Set<Country> countries) {
          this.countries = countries;
       }
    
    }
    Then in your view page, iterate over the Collection. The following code is taken from the PetClinic sample application's war/WEB-INF/jsp/visitForm.jsp file:

    Code:
    <B>Previous Visits:</B>
    <TABLE border="true">
      <TH>Date</TH><TH>Description</TH>
      <c:forEach var="visit" items="${visit.pet.visits}">
        <c:if test="${!visit.new}">
          <TR>
            <TD><fmt:formatDate value="${visit.date}" pattern="yyyy-MM-dd"/></TD>
            <TD><c:out value="${visit.description}"/></TD>
          </TR>
        </c:if>
      </c:forEach>
    </TABLE>
    In the above case, the view is iterating over a pet's Set of Visits.

    So by having a Set of Country objects in a Person, you can then iterate over the Countries in your view via: person.countries. There are other issues, such as how to provide the Person object to the view for outputting, but that is a different story.

    If this did not answer your question, please re-post.

    -Arthur Loder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •