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

  • How to write a PropertyEditor?

    Hello,

    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:

    Code:
    <c:forEach var="friend" items="${friends}" varStatus="index">
    <tr>
    	<td>
    		<spring:bind path="friend.action">
    			<input type="hidden" name="${status.expression}" value="<%= com.upsnap.bus.Friend.ACTION_REMOVE %>"/>
    		</spring:bind>
    		<spring:bind path="friend.checked">
    			<input type="checkbox" name="${status.expression}"/>
    		</spring:bind>
    		<spring:bind path="friend.userName">
    			<input type="hidden" name="${status.expression}" value="${friend.userName}"/>
    		</spring:bind>
    		<spring:bind path="friend.since">
    			<input type="hidden" name="${status.expression}" value="${friend.since}"/>
    		</spring:bind>
    	</td>
    </tr>
    </c:forEach>
    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:
    Code:
    if (command instanceof Friend[])
          doMethod1();
    else if (command instanceof Friend)
          doMethod2();
    doMethod2() is executed, so obviously what I submit is not an array. Still, this is not a problem!

    I do like this:
    Code:
    Friend friend = (Friend)command;
    log.info("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.
    Code:
    class FriendsArrayEditor extends PropertyEditorSupport {
    		
    		public void setAsText(String text) throws IllegalArgumentException {
    			// TODO Auto-generated method stub
    			super.setAsText(text);
    		}
    		
    		public String[] getTags() {
    			// TODO Auto-generated method stub
    			return super.getTags();
    		}
    	}
Working...
X