Results 1 to 3 of 3

Thread: How to iterate over a Set of Objects present in ModelAttribute?

  1. #1
    Join Date
    Feb 2011
    Location
    Software Nirvana Land
    Posts
    30

    Exclamation How to iterate over a Set of Objects present in ModelAttribute?

    I've a Hibernate Entity class:

    Code:
    @Entity
    @Table(name = "user_details")
    public class UserDetails implements java.io.Serializable {
    
    	private static final long serialVersionUID = 1L;
    	private Integer userDetailsId;
    	.
    	.
    	some other fields
    	.
    	.
    	private Set<UserExperience> userExperiences = new HashSet<UserExperience>(0);
    	
    	.
    	some getter and setters
    	.
    	.
    	
    	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    	@JoinTable(name = "details_experience", joinColumns = { @JoinColumn(name = "UserDetailsID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "UserExperienceID", nullable = false, updatable = false) })
    	public Set<UserExperience> getUserExperiences() {
    		return this.userExperiences;
    	}
    
    	public void setUserExperiences(Set<UserExperience> userExperiences) {
    		this.userExperiences = userExperiences;
    	}
    }
    I've a JSP where I'm showing these fields for the user to edit.

    Code:
    <c:url var="saveUrl" value="editprofile_applicant.html" />
    <form:form modelAttribute="userDetailsAttribute" method="POST" action="${saveUrl}">
    		
    <form:label path="userDetails.userExperiences">Experience</form:label>
    <form:textarea path="userDetails.userExperiences" id="userExperience" />
    		
    <input type="submit" value="Save Changes"/>
    
    </form:form>
    Every other field is working fine. But the Experience textarea shows the UserExperience object's name instead of UserExperience's name.

    But if the user enters anything in the textarea and saves, it is bind correctly and saved correctly to the Database.

    The UserExperience entity is as follows:
    Code:
    @Entity
    @Table(name = "user_experience")
    public class UserExperience implements java.io.Serializable {
    
    	private static final long serialVersionUID = 1L;
    	private Integer userExperienceId;
    	private String experienceName;
    
    .
    .
    .
    I want to display experienceName in the textarea for the first time. How cani do that?

    How to iterate over "userDetails.userExperiences" cos it's a List of <UserExperience> objects. How to loop through it and show a number of texareas?

    I know I can save the List in a map and then access that using EL.

    Code:
    <c:forEach  items="${useExperience}" var="experience" >
       <input type="text" value="${experience.experienceName}" name="myexperience" />
    </c:forEach>
    But then i can't save it using path binding. Cos finally I do a session.save(UserDetails).

    Please guide me. I'm stuck.

  2. #2
    Join Date
    Feb 2011
    Location
    Software Nirvana Land
    Posts
    30

    Exclamation

    I did some research and changed my JSP to the following:
    Code:
    <c:forEach items="userDetails.userExperiences" varStatus="index">
    			
    <form:textarea path="userDetails.userExperiences[${index.count - 1}].experienceName"  />
          	
    </c:forEach>
    Now it shows correct data inside the textarea. (Please see attachment)

    scrn.png

    But when i try to save it bu submitting the form, i get the following error:

    Code:
    Cannot get element with index 0 from Set of size 0, accessed using property path 'userExperiences[0]'
    Please help me.

  3. #3

    Default

    they said that hashset cannot be bind because form binding uses index to know the order
    and since hashset do not have index, so you cannot use it..

    i have the same problem right now.

    some suggested solution to use @Transient and using List.. and I will try that..
    Please let us know if you had solved already.
    Thanks.

Posting Permissions

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