Search code examples
selectdrop-down-menutapestry

Tapestry: default value for a dropdown component


I use the following code for a select-component:

Java-class:

@Component(parameters = {"blankOption=AUTO", "model=someModel", "value=someId",
                         "zone=someZone"})
private Select demoSelect;

Template:

<select t:id="demoSelect" />

This gets rendered to something like the following:

<select id="demoSelect" name="demoSelect">
    <option value=""></option>
    <option value="1">first</option>
    <option value="2">second</option>
    <option value="3">third</option>
</select>

The behavior I'm looking for is, that a certain option is preselected (this should be decided in the page class). How can I configure this in Tapestry? Basically I need to tell Tapestry to render the "selected" for the appropriate option, e.g.:

<select id="demoSelect" name="demoSelect">
    <option value=""></option>
    <option value="1">first</option>
    <option value="2" selected="selected">second</option>
    <option value="3">third</option>
</select>

Does it suffice to alter the model (I don't think so), or do I have to extend the Select-component itself. I have found this article, which looked quite promising, but unfortunately all links to the source codes are dead.


Solution

  • There is no need to extend anything. Just setting the property to a value before the rendering does the trick:

    @Property
    private SomeType someId;
    
    @SetupRender
    void initSomeId() {
        if (this.someId == null) {
           this.someId = this.getDefaultValueForSomeId();
        }
    }