Announcement Announcement Module
Collapse
No announcement yet.
Usage of the <form:select> tag Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Usage of the <form:select> tag

    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, 01:51 PM.

  • #2
    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.

    Comment


    • #3
      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?

      Comment


      • #4
        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!

        Comment


        • #5
          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?

          Comment


          • #6
            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, 06:29 PM.

            Comment


            • #7
              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, 06:30 PM.

              Comment


              • #8
                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!

                Comment


                • #9
                  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

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      All right then:

                      Originally posted by pmularien View Post
                      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").
                        I have done this already
                      2. Display the form. Is the correct value selected from the list?
                        No, it's not
                      3. Submit the form. When the form redisplays, is the correct value selected from the list?
                        Nope. But that's because my success page is different. I know I told it was the same. My bad! Sorry.
                      Having clear answers to these questions will help make sure we're on the same page, and that I understand what you're trying.
                      To make sure we're on the same page, I'll detail even more my workflow:
                      1- User enters the site and a form with a list of categories is shown.
                      2- User click the edit button in one of the categories. The edit form(and this one is the same for inclusion) is presented, but the parent category is not selected from the list (assume it's mapped to parentCategory only)
                      --
                      1- User enters the site and a form with a list of categories is shown.
                      2- User clicks the create button. The insert form(the same) is presented and the inclusion works great

                      Please do tell me if you need any further info!

                      Thanks a lot for your help!

                      Comment


                      • #12
                        Asa9

                        Hi,

                        I have the same problem.

                        The value from the option list is not selected, when I try to edit one of the entries of the list .
                        But the insertion works fine.

                        I'm looking for a solution for already 2 days, but can't find yet .

                        Have you find anything?

                        Thanks,
                        ASA9

                        Comment

                        Working...
                        X