Results 1 to 4 of 4

Thread: spring-form tag lib's select tag does not work

  1. #1
    Join Date
    May 2009
    Posts
    246

    Default spring-form tag lib's select tag does not work

    I'm trying to get spring's select tag to work in spring 3.0.0rc1, and it doesn't work.

    Code:
                <@springForm.select path="myobject.province" items=provinces multiple=false
                    itemLabel="name" itemValue="id" />
    This is the code called from freemarker. everything else works out of freemarker except for this, and I doubt freemarker is the problem.

    The problem is that when the form loads, the spring select tag does not set the initial value, even though it is 100% set. I can verify this by outputting ${myobject.province.id} and it will give an id like 6. Since there is most definitely an object in "provinces" list with an id of 6, it should add selected="selected". However, it is not.

    I know this is a problem with the tag because it has 100% nothing to do with my code. Upon selecting an option, it gets bound properly through the property editor and eventually arrives in the databases. So basically, the problem is with the tag itself.

    I have made my own rudimentary <Select> and <options> that work... so I dunno why Spring's tag doesn't work.

    New Bug introduced in rc1?

  2. #2
    Join Date
    May 2009
    Posts
    246

    Default

    Unfortunately, I had to write a macro which contains the following code... which actually works sort of:

    Code:
    <#assign status = springMacroRequestContext.getBindStatus( commandName+"."+path+".id" ) />
            <#assign selectedValue = status.value!"" />
    
            <select id="${status.expression}" name="${status.expression}">
                <#list items as item>
                    <option value="${item.id}"<#if selectedValue == item.id>selected="selected"</#if>>${item[label]}</option>
                </#list>
            </select>
            <br/>
    
            <@springForm.errors path=path cssClass="error" />
    my macro doesn't work if the value of the full path is null. Your getBindStatus decides to throw an exception rather than just return null... so freemarker can't trap the exception... so I am basically screwed.

    Also, passing in:

    Code:
    commandName + "." + path
    ... without the extra ".id" results in getting a String value of 2. I get this string value of 2 no matter what the id is set to.

    However, simply making a spring-form:input tag with the same path results in EXACTLY what the id is set to - basically the output of freemarker. So your code to determine if the value is selected is clearly incorrect.

    There is clearly something wrong with the tag library.

    Please just fix the tag library. I mean, this is something so simple and trivial and I have spent nearly 4 or 5 hours trying to fix your tag library with no success. I honestly feel like I cannot proceed with the work I must do to get the project done, which is a really bad feeling to have. There's just nothing I can do to fix this on my own.

  3. #3
    Join Date
    May 2009
    Posts
    246

    Default

    Here's the property editor:
    Code:
    public class DomainObjectEditor extends PropertyEditorSupport {
    
           private BaseDaoSupport dao;
           private SimpleTypeConverter typeConverter = new SimpleTypeConverter();
    
           public DomainObjectEditor( BaseDaoSupport dao ) {
                   this.dao = dao;
           }
    
           @Override
           public String getAsText() {
               Object obj = getValue();
               if( obj == null ) {
                   return null;
               }
    
                   if( obj instanceof DomainObject ) {
                           DomainObject domainObject = ( DomainObject ) obj;
    
                           return typeConverter.convertIfNecessary(
                                   domainObject.getId(), String.class );
                   }
    
                   throw new IllegalArgumentException( "Value must be a DomainObject" );
           }
    
           @Override
           public void setAsText( String text ) {
               if( text == null || 0 == text.length() ) {
                   setValue( null );
                   return;
               }
    
                   setValue( dao.find(
                           typeConverter.convertIfNecessary( text, Long.class ) ) );
           }
    
    }
    It works perfectly. In fact, even though the tag is not properly selecting the value, spring mvc is still binding the selected option. Spring mvc is also binding the correct value as well when it goes to freemarker.

    Man, I hate bugs like this. this is totally a bug in spring. I wonder how long it's been here. It's so annoying.

  4. #4
    Join Date
    May 2009
    Posts
    246

Posting Permissions

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