Binding selected items (multiple select box)
Hello,
I'm actually really stuck with binding an n:m object graph to a multiple select box.
Consider the following domain model (hibernate beans):
Code:
public class User
{
private Integer id;
private String username;
private List<UserRole> userRoles;
// Standard setters and getters
}
public class Role
{
private Integer id;
private String name;
// Standard setters and getters
}
public class UserRole
{
private User user;
private Role role;
// Standard setters and getters
}
As you see the n:m relation is based on two 1:n relations (User 1:n UserRole n:1 Role).
In my form I want two multiple select boxes - one box should show all unassigned roles, the other one should show the assigned ones.
JavaScript buttons should manage the assignment / disassignment (shift entries from the unassigned box to the assigned box, or the other way around).
My problem is, that I don't really know how to manage this in a professional way. I'd know how to do that manually in onBind(), but I thought it would be more professional to handle that by a custom PropertyEditor. The first thing with a custom PropertyEditor which I don't know is, to which Class I should bind it (List.class, UserRole.class, Role.class). Furthermore I don't know how the html code should look like (the spring related part - spring:bind, spring:transform or whatever).
I read (and mostly understand) "Spring in Action", "Pro Spring", " Professional Java Development with the Spring Framework", as well es the online documentation and tons of forum topics. Nevertheless I feel totally innocent how this problem could be handled. I found many forum topics about binding multiple select boxes, but I couldn't find one that really helped.
I'd really much appreciate any help!
Best
Oliver
Dynamic binding to a HashSet
I have a slightly different problem.
I have a hibernate domain object as follows
Code:
public class TestModel
{
Set s;
......getters and setters
}
The HashSet in the TestModel contains the following objects (say 4 of them)
Code:
public class City
{
private String city;
private String name;
......getters and setters
}
I use the formBackingObject to populate the values so that they get displayed on the form
Code:
protected Object formBackingObject(HttpServletRequest arg0)
throws Exception
{
TestModel testModel = new TestModel();
Set s = new HashSet();
City city = new City();
city.setCity("A");
city.setName("B");
s.add(city);
city = new City();
city.setCity("C");
city.setName("D");
s.add(city);
city = new City();
city.setCity("E");
city.setName("F");
s.add(city);
city = new City();
city.setCity("G");
city.setName("H");
s.add(city);
testModel.setS(s);
return testModel ;
}
The jsp page uses this code to bind and show the values
HTML Code:
<spring:bind path="testModel.s">
<core:forEach items="${status.value}" var="obj">
<input type="text" value="<core:out value="${obj.city}"/>" name="<core:out value="${status.expression}"/>City">
<input type="text" value="<core:out value="${obj.name}"/>" name="<core:out value="${status.expression}"/>Name">
</core:forEach>
<div id="myDiv">
<input type="hidden" value="0" id="theValue" />
</div>
<tr>
<input type="button" name="addRow" value="add" onclick="addElement('<core:out value="${status.expression}"/>City','<core:out value="${status.expression}"/>Name');">
</tr>
</spring:bind>
After getting displayed, the jsp now contains
The values within the objects in the "languages" HashSet are displayed in the jsp as
City Name
--- -----
A B
C D
E F
G H
----------
| AddRow | <-- The add row button
----------
The actual "names" of the textboxes for City and Name come out to be "sCity" and "sName" respectively.
By using the "addRow" another row is dynamically inserted to the table with the same names as the previous textboxes. Within the jsp these new textboxes come within the <spring:bind> tag.
But when the form is submitted these do not show up in the model. Which means that, if there are already 4 cities in the model and i add a fifth dynamically, the fifth is not shown when displayed within the onSubmit() method after being extracted from the model.
Code:
protected ModelAndView onSubmit(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, BindException arg3)
throws Exception
{
TestModel testModel = (TestModel) arg2;
System.out.println("hash SIZE = " + tm.getS().size());
.....
}
The size is still shown as 4 instead of 5.
I have been searching without success in this forum for a solution to this problem .
What am I doing wrong ?
Someone help me out on this.