reset password
Author Message
cysun
Posts: 2935
Posted 09:28 Aug 12, 2015 |

In the last couple of days several students asked about implementing the dropdown list for committee chair and the checkboxes for committee members in the midterm. The key points are as follows:

Using Spring <form> tags to bind the properties of a Job object to the fields of the form makes implementation a lot easier. However, the "value" attributes of HTML form tags are strings, which means a string property of Job like name can be easily bound to a form field with <form:input>, but an object property of Job like committeeChair cannot, and this is where property editors come in.

A property editor translates between an object and a string. Spring provide some property editors like the CustomDateEditor. You can also write your own property editors. You can find a number of property editors in the csns.web.editor package in the CSNS2 project.

To tell Spring to use a property editor, you have to register it for a controller. You can either register it for single controller class by adding an @InitBinder method to the class, or register it for all controllers by configuring it in the bean configuration file.

After that you can bind the object property more or less like binding a string property.

And here are the steps to implement the dropdown list and the checkboxes in the midterm:

1. Implement a User property editor. You can just copy over the one in CSNS2.

2. Register the editors with the Job controller:

    @Autowired
    private WebApplicationContext context;

    @InitBinder
    public void initBinder( WebDataBinder binder )
    {
        binder.registerCustomEditor( User.class,
            (UserPropertyEditor) context.getBean( "userPropertyEditor" ) );
        binder.registerCustomEditor( Date.class,
            new CustomDateEditor( new SimpleDateFormat( "M/d/yyyy" ), true ) );
    }

A property editor is an object, which you can create either by calling the constructor, or create it as a Spring bean (review the discussion of the Hello World example in the Spring IoC lecture). For a property editor like the date property editor, we can simply use the constructor, but for the User property editor, we need to create it as a bean because it needs a UserDao which needs to be injected by the Spring framework.

3. Bind the properties using Spring <form> tags:

<tr>
  <th>Committee Chair</th>
  <td>
    <form:select items="${reviewers}" path="committeeChair" itemValue="id" itemLabel="name" />
  </td>
</tr>
<tr>
  <th>Committee Members</th>
  <td>
    <c:forEach items="${reviewers}" var="reviewer">
    <form:checkbox path="committeeMembers" value="${reviewer}" />
    ${reviewer.name} <br />
    </c:forEach>
  </td>
</tr>

Please read the <form> taglib documentation for the meanings of the attributes.

Note that HTTP allows multiple request parameters to have the same name. For example, something like ...?a=10&a=20&a=30, or the checkboxes in the code above. Naturally this could be bound to a list or an array property and Spring knows how to do that. So in terms of property editor, we just need to provide one for User, and Spring will be able to handle List<User>.

bseemscs
Posts: 26
Posted 17:29 Aug 16, 2015 |

Where does ${reviewers} come from?

cysun
Posts: 2935
Posted 18:32 Aug 16, 2015 |
bseemscs wrote:

Where does ${reviewers} come from?


Controller.

Last edited by cysun at 18:32 Aug 16, 2015.
bseemscs
Posts: 26
Posted 19:37 Aug 16, 2015 |

Yeah but, I mean, how? Is it something like models.put( "reviewers", jobDao.getJob ( id ).getCommitteeMembers() ) ? And how is the userPropertyEditor tied in? 

1. I created the UserPropertyEditor in a csjobs.web.editor package like the one in CSNS2

2. I have the registered the editor in the job controller using the @Autowired part and the @InitBinder method

3. I have the spring <form> tags installed in the jsp

But I am missing something about how to tie everything together. Am I supposed to use userPropertyEditor.something ? Is there supposed to some xml file like in the "hello.xml" file in the video? There seems to some crucial piece of information that is missing. Looking at CourseControllerS in CSNS2 and not seeing how the property editor is being used.

cysun
Posts: 2935
Posted 20:20 Aug 16, 2015 |

You don't need to use property editor explicitly - it's used by Spring to bind object properties to form fields. Read this. And if you are not clear about the attributes of the <form> tags, read the documentation.