wood burning stoves*
The moose likes Spring and the fly likes How do I use <form:form> with an Object that contains a Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How do I use <form:form> with an Object that contains a Set" Watch "How do I use <form:form> with an Object that contains a Set" New topic
Author

How do I use <form:form> with an Object that contains a Set

laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
I have used <form:form> before on an object but never with an object that contains a Set of objects.

For example, I have a Person object with attributes such as id, firstname, lastname, email, and a Set of Programs.

The Program object contains the attributes: id, code, name, date, status

If I have a form that is bound to the Person object with a checkbox of their Program.status how do I set the path of the <form:checkbox> to link with the Program's "status" attribute? Say the "status" contains a Yes/No value... if I want to bind the user's value of checkbox to the Program.status I have no idea how to bind this attribute.

Any ideas?

This is what I have right now:




Thanks
Himanshu bisht bebo
Greenhorn

Joined: Jun 12, 2012
Posts: 16
I thinks this will work for you

<form:form method="post" commandName="person" action="${actionUrl}">

<c:forEach items="${person.programs}" path="programs" var="program">
<form:checkbox value="Yes"/>${program.name}<br>
</c:forEach>
<form:hidden path="id"/>
<button type="submit" >Submit</button>
</form:form>

laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108

I'm wondering if I would also need to put the path to the attribute beside the checkbox like this since I need to basically populate programs.status:




I'll test it and will post back with my findings.

Thanks for the idea
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Oh, it looks like that won't work. I get an error on <c:forEach> where attribute path is invalid for tag foreach.

hmm...

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7


http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/view.html#view-jsp-formtaglib-checkboxestag
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/spring-form.tld.html#spring-form.tld.checkboxes


[How To Ask Questions][Read before you PM me]
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Thanks for your assistance, I think I almost got it but I think I may need a little more clarification.

I think my issue now may be the way that I am using the checkboxes..perhaps my logic is not correct.

So right now I have my form



Here are my objects





My viewController is working fine and takes me to my jsp form but the processing of my form is failing. Once I click on submit I get this error:
org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred while calling setter of com.domain.Person.programs; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.domain.Person.programs
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.domain.Person.programs
Caused by: java.lang.IllegalArgumentException: argument type mismatch


My idea is to populate program.status with a "Yes" when checked. By default, the value of program.status is "No". But it just doesn't make sense to me how that value "Yes/No" is passed through <form:checkboxes>. I have been reading different examples but I can't seem to get it right.

Here is my submitController that is processing the results which I'm sure is incorrect:


My table data looks like this

Person Table
id: 1234
username:jdoe
email:jdoe@gmail.com
loginDate: 2013-06-20


Program Table

id: 1
name:Swim
code:SWM
status:NO
endDate:2013-05-31
pid: 1234

id: 2
name:Tennis
code:TEN
status:NO
endDate:2013-05-31
pid: 1234


Any advice as to how the processing should work in my case?

Thanks
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Spring will only send the ids for the programs that are checked. Also any field not bound to the form (using path) as part of the form or as a hidden field is not sent. You will need to register a custom property editor or use a converter to get the submitted id's to actual role objects. Your error is due to the fact that Spring does not know how to do that conversion without you specifying it.

Have a look at these links:
http://stackoverflow.com/questions/7421346/spring-binding-listobject-to-formcheckboxes
http://stackoverflow.com/questions/6989711/spring-mvc-pre-populate-checkboxes/6996350#6996350
http://stackoverflow.com/questions/12544479/spring-mvc-type-conversion-propertyeditor-or-converter
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
I have been reading several examples and I think it's almost there. I created my InitBinder and CustomPropertyEditor and I am no longer getting the argument mismatch instead I am getting this error:


Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'person' on field 'programs': rejected value [68]; codes [methodInvocation.person.programs,methodInvocation.programs,methodInvocation.java.util.List,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.programs,programs]; arguments []; default message [programs]]; default message [Property 'programs' threw exception; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.domain.ProgramEditor]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.domain.ProgramEditor.<init>()]


I'm thinking it's because the form is passing a list of programs from the checkboxes and maybe it doesn't know how to translate it?

This is what I have now:


And here is my PropertyEditor:



Is there something else I need to add that I'm not considering?

Thanks
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7



Can you show me the code where you create programEditor? It looks like from the stack trace that it is expecting ProgramEditor to have a default constructor for some reason. This should not be necessary unless you are not passing in IPersonStore when you create it.
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Here it is:


Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

You might have other issues but lets try to solve this one first. I don't see the need to implement InitializingBean. Lets try this: (note I did not see an @Controller annotation so I added it. Typically you would have a component scanner scanning this package in your context. Not sure why it was not there). The setters are also not necessary so I removed them.

laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Okay, I am no longer receiving an error after I revised my PropertyEditor. As a matter of fact, I followed the example from this url because it's very similar to mine: http://stackoverflow.com/questions/4331532/multiple-select-in-spring-3-0-mvc

Now, I'm just a little confused now how to use it. After I check one of the boxes and click submit I see my Hibernate query statement which is retrieving the program id correctly. So, is it here in my CustomEditor that I need to set the value of status='Yes' and not in my actionAddProgramForm() method?

Hibernate: select program0_.id as id1_, program0_.name as name1_, program0_.code as code1_, program0_.status as status1_, program0_.endDate as endDate1_, program0_.pid as pid1_ from program program0_ where program0_.id='92'

Here is the new editor that I used that is no longer giving me an error:




I feel so close to having this working...I'm so excited

Thanks
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

I guess if the goal is to set the status to "Yes" whenever a checkbox is checked your could set it there.

I assume when you say "Should I use a hibernate method that sets the "status" attribute here? " You mean just call the setter right?



Assuming cascade rules are set up correctly you could just iterate the returned <Set> and add them to your persons program collection and then persist/merge the person which would also update that status at that point. Anyway good luck.
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Thanks Bill so much for your help and guiding me through this challenge. Last night I finally got everything working...woohoo!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I use <form:form> with an Object that contains a Set
 
Similar Threads
Problem with CheckBoxes
Wrapped list in command object
Spring MVC checkbox issue
Validation errors not displayed
List of checkboxes and Array/ArrayList data binding not working.