Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Usage of the <form:select> tag

  1. #1

    Default Usage of the <form:select> tag (editing doesn't work)

    Hey guys,

    This is what I've got so far.

    I have a JSP that I use both for inserting a new record and editing a existing one. It looks like this:

    Code:
    <body>
    <form:form commandName="category">
        <table>
            <tr>
                <td>Description:</td>
                <td>
                	<form:input path="description"/><form:errors path="description"/>
    				<form:select path="parentCategory">
    						<option value="">Select...</option>
    			            <form:options items="${parentCategoryList}" itemValue="id" itemLabel="description"/>
    				</form:select>
                </td>
                <td></td>
            </tr>
            <tr>
                <td colspan="3">
                    <input type="submit" value="Save Changes"/>
    				<input type="button" value="Cancel" onclick="cancel();"/>
                </td>
            </tr>
        </table>
    </form:form>
    </body>
    My command class, Category, is as follows:

    Code:
    public class Cateogry {
        private Long id;
        private String description;
        private Category parentCategory;
        ...
    }
    In my controller, I have written a custom property editor to handle this command:

    Code:
    @Override
    	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    		binder.registerCustomEditor(Category.class, "parentCategory", 
    				new PropertyEditorSupport() {
    					@Override
    					public String getAsText() {
    						if (this.getValue() != null && this.getValue() instanceof Category) {
    							return ((Category)this.getValue()).getId().toString();
    						}
    						return this.getValue() == null ? "" : this.getValue().toString();
    					}
    			
    					@Override
    					public void setAsText(String text) throws IllegalArgumentException {
    						if (text != null && !text.equals("")) {
    							setValue(categoryService.get(new Long(text)));
    						} else {
    							setValue(null);
    						}
    					}
    					
    			
    				});
    	}
    The problem is that, the way it is, I can insert a record, save the association with its parentCategory and so on but, when I retrieve the record from the database in my formBackinObject() to display in the view, the parentCategory property, which is a drop-down, does not come with the actual value selected. It always come with the "Select..." option selected.

    I have already verified that the parentCategory is being correctly populated before returning the view, and it is.

    Any thoughts?

    Thanks!
    Last edited by leonardoborges; Mar 27th, 2007 at 12:51 PM.
    Leonardo Borges

  2. #2

    Default

    Also, the only way a managed it to work when editing was when I changed

    Code:
    				<form:select path="parentCategory">
    						<option value="">Select...</option>
    			            <form:options items="${parentCategoryList}" itemValue="id" itemLabel="description"/>
    				</form:select>
    to this:

    Code:
    				<form:select path="parentCategory.id">
    						<option value="">Select...</option>
    			            <form:options items="${parentCategoryList}" itemValue="id" itemLabel="description"/>
    				</form:select>
    But then it unleashes several other issues and I don't think it's the right way of doing this.
    Leonardo Borges

  3. #3

    Default

    I'm starting to think that the only way to solve this is to have a jsp for insert, binding to parentCategory AND a jsp for edit, binding it then to parentCategory.id.

    any other solutions?
    Leonardo Borges

  4. #4
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Ah, I recognize your problem now. I believe you're running into the classic Spring MVC problem of the property editor sticking around after the form has been submitted. Try changing your success view to a redirect back to the same page, and see if that helps. Unfortunately there's not a way to "unregister" a property editor upon a successful submit, besides with a redirect (which is advisable anyway, as it tends to lend itself to a nice PRG pattern). Good luck!
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  5. #5
    Join Date
    Apr 2005
    Location
    Finland
    Posts
    314

    Default

    Quote Originally Posted by leonardoborges View Post
    I'm starting to think that the only way to solve this is to have a jsp for insert, binding to parentCategory AND a jsp for edit, binding it then to parentCategory.id.

    any other solutions?
    This is exactly how I have done the bindings in selection boxes. I also had a custom property editor class, but I noticed that I didn't need it

    What kind of problems did you find with this solution?
    if a trainstation is where the train stops, what's a workstation...

  6. #6

    Default

    Quote Originally Posted by pmularien View Post
    Ah, I recognize your problem now. I believe you're running into the classic Spring MVC problem of the property editor sticking around after the form has been submitted. Try changing your success view to a redirect back to the same page, and see if that helps. Unfortunately there's not a way to "unregister" a property editor upon a successful submit, besides with a redirect (which is advisable anyway, as it tends to lend itself to a nice PRG pattern). Good luck!
    I think I didn't get it.

    The problem is not with the success view, but with the form view that does not bring the right element selected in the dropdown.

    Am I missing something?
    Last edited by leonardoborges; Mar 27th, 2007 at 05:29 PM.
    Leonardo Borges

  7. #7

    Default

    Quote Originally Posted by noon View Post
    This is exactly how I have done the bindings in selection boxes. I also had a custom property editor class, but I noticed that I didn't need it

    What kind of problems did you find with this solution?
    Why did u realize you didn't need it?

    Also, there is no problem with this solution, just one more file around.
    Last edited by leonardoborges; Mar 27th, 2007 at 05:30 PM.
    Leonardo Borges

  8. #8
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Quote Originally Posted by leonardoborges View Post
    I think I didn't get it.

    The problem is not with the success view, but with the form view that does not bring the right element selected in the dropdown.

    Am I missing something?
    If you have bound the PropertyEditor you posted in your controller's initBinder method, you should not need the ".id" in the bind path, because it is the job of your PropertyEditor to represent the bound property for rendering in the page (in this case, using the "id" property). I'm not really sure how to help further, other than to say this is definitely possible to get working as you'd expect, and using the bind path including the ".id" property is not the right solution. Hopefully that helps!
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  9. #9

    Default

    pmularien,
    I totally agree that it's not the right solution to have it binding to parentCategory.id

    But, as you can see, I don't think it is something wrong with my property editor but, imagine I have 3 categories: Database, Presentation, SQL.

    Let's say SQL's parent is Database. If I bind to parentCategory, instead of .id, when presenting the record in the form view, the drop down doesn't come with the parent Database selected, just the default "Select..." option.

    The only way I managed to solve this, althought it isn't correct, is to have 2 distinct jsp's, one for insert, binding to parentCategory, and the other for updates, binding to parentCategory.id

    Do you have a working different example you could share with us?

    Thanks
    Leonardo Borges

  10. #10
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    I don't have an example I can pull together at the moment, although I can try to do so. Let me try to help you work through your scenario, though.

    Please do / answer the following:
    1. Change your JSP so that you are binding to "parent" (NOT "parent.id").
    2. Display the form. Is the correct value selected from the list?
    3. Submit the form. When the form redisplays, is the correct value selected from the list?
    Having clear answers to these questions will help make sure we're on the same page, and that I understand what you're trying.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •