Struts2 Map Form to Collection of Objects

July 2, 2009 - 4 minute read -
orm Java struts2 jsp OGNL

The Struts2 documentation contains examples that are often basic at best which can make it challenging to figure out how to do things sometimes. I was working on creating a form that would allow me to select values from a list to connect 2 objects in a One-to-Many relationship. This is a common relationship for many things. In this example, I'll use a User and Role class to demonstrate the concept.

For background, here's a JPA mapped User and Role class.

import java.util.List;
import javax.persistence.*;</p>
<p>@Entity
public class User {</p>
<p>    private Long id;
    // ... other member variables
    private List<Role> roles;</p>
<p>    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }</p>
<p>    public void setId(Long id) {
        this.id = id;
    }</p>
<p>    @OneToMany
    @JoinTable(name = "UserRoles",
            joinColumns = @JoinColumn(name = "user_Id"),
            inverseJoinColumns = @JoinColumn(name = "role_Id"),
            uniqueConstraints = @UniqueConstraint(columnNames = {"user_Id", "role_Id"})
    )
    public List<Role> getRoles() {
        return roles;
    }</p>
<p>    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }</p>
<p>    // ... other properties
}</p>
<p>@Entity
public class Role {</p>
<p>    private Long id;</p>
<p>    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }</p>
<p>    public void setId(Long id) {
        this.id = id;
    }</p>
<p>    // ... other properties
}

A list of Roles exists in the database. When a User is created, they are assigned one or more Roles. The User and Roles are connected in the Database through a join table as defined in the mapping. At this point I created some DAO Repository classes to manage the persistence and an Action to handle the form values. (The details of JPA, setting up Persistence and Actions are beyond the scope of this post.)

The part that caused me the most grief ended up being the form. I wanted to add a checkbox list that contained all of the Roles. The example on the Struts site for checkboxlist, a control with 43 documented properties is:

<s:checkboxlist name="foo" list="bar"/>
Needless to say, there was some 'figuring out' to be done.

The form itself is pretty vanilla for the most part. The checkboxlist is the interesting part because it's what allows us to map the User to the Roles. I knew that I was looking for something to put into the value property of the control that would tell it to pre-select the Role values that were already associated with the User.

I started out with something like:

<s:checkboxlist name="user.roles.id"
                    list="roles"
                    listKey="id"
                    listValue="name"
                    label="%{getText('label.roles')}"
                    value="user.roles"/>

That didn't work. When you think about it, that makes sense because the keys in the list are ids and the values supplied are Role objects. So I needed to figure out how to get the Ids from the Roles. I could have done that in the Action class, but it seemed like there should be a better way. A way that would allow me to continue in more of a Domain fashion.

Doing some research into OGNL, I came upon the list projections section which was the key...

The OGNL projection syntax gives us user.roles.{id}. Basically that is a list comprehension that takes of list of Role objects and turns it into a list of Role Ids. That list of Ids becomes the list of values that will be preselected.

Knowing that I can now create a select box that will include the pre-selected values on an edit form:

<s:checkboxlist name="user.roles.id"
                    list="roles"
                    listKey="id"
                    listValue="name"
                    label="%{getText('label.roles')}"
                    value="user.roles.{id}"/>

Enjoy.