Announcement Announcement Module
No announcement yet.
How to write a PropertyEditor? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to write a PropertyEditor?


    I have the following situation: ControllerOne creates an array of Friend's and submits it to a JSP page like this:

    Friend[] friends = getFriends();
    return new ModelAndView("friends.jsp", "friends", friends);

    On the JSP page, I have a form like this:

    <c:forEach var="friend" items="${friends}" varStatus="index">
    		<spring:bind path="friend.action">
    			<input type="hidden" name="${status.expression}" value="<%= com.upsnap.bus.Friend.ACTION_REMOVE %>"/>
    		<spring:bind path="friend.checked">
    			<input type="checkbox" name="${status.expression}"/>
    		<spring:bind path="friend.userName">
    			<input type="hidden" name="${status.expression}" value="${friend.userName}"/>
    		<spring:bind path="friend.since">
    			<input type="hidden" name="${status.expression}" value="${friend.since}"/>
    I have been told that this is not the best way to do this, but anyway this works. When I click on the Submit button, the form is submitted to another controller, ControllerTwo.

    OK, so let's say I have two friends: Joe and Billy. In ControllerTwo I do this:
    if (command instanceof Friend[])
    else if (command instanceof Friend)
    doMethod2() is executed, so obviously what I submit is not an array. Still, this is not a problem!

    I do like this:
    Friend friend = (Friend)command;"User name: " + friend.getUserName());
    The result in the log will be:
    User name: Joe,Billy
    So obviously the two are concatenated because, in the <form> of the JSP page, both fields have the same name.

    My solution to this problem would be writing a custom PropertyEditor that would set them appart and recreate the array of Friend's. So could someone explain to me how to do this?

    I think I should go from something like this, but I don't see how can I recreate a complete Friend object from this.
    class FriendsArrayEditor extends PropertyEditorSupport {
    		public void setAsText(String text) throws IllegalArgumentException {
    			// TODO Auto-generated method stub
    		public String[] getTags() {
    			// TODO Auto-generated method stub
    			return super.getTags();