PropertySelection

Creates form elements that allow a property of an object to be set from a drop-down list.

Uses a IPropertySelectionModel to map between Java values that will be assigned, and textual labels that will appear in the HTML response.

A useful property selection model is available ( StringPropertySelectionModel ). You can also create your own model, as illustrated in the examples below.

Note that complex value Objects need to properly implement the Object.equals() method if the correct initial item is to be displayed.

Informal parameters are applied to the <select> tag.  To have greater control over the <option> tags, you must use the Select and Option components.

See also: org.apache.tapestry.form.PropertySelection , Select , Option , Radio , RadioGroup , Form , Script

Parameters

Name Type Direction Required Default Description
value Object in-out yes The property to set. During rendering, this property is read, and sets the default value of the selection (if it is null, no element is selected). When the form is submitted, this property is updated based on the new selection.
model IPropertySelectionModel in yes The model provides a list of possible labels, and matches those labels against possible values that can be assigned back to the property.
disabled boolean in no false Controls whether the <select> is active or not. A disabled PropertySelection does not update its value parameter. Corresponds to the "disabled" HTML attribute.
submitOnChange (deprecated) boolean in no false If true, then additional JavaScript is added to submit the containing form when select is changed. Equivalent to specifying a JavaScript event handler of this.form.submit() .
displayName String in no The user-presentable name for the component, which will be used by a FieldLabel connected to the component.
validators Array or collection of Validator , or Validator in no The validators to apply to the component.
id String in no The value of the id attribute in the generated <select> tag.

Body: removed

Informal parameters: allowed

Reserved parameters: name

Examples

Example 1

The PropertySelection component provides Gender selection drop down list using a StringPropertySelectionModel

<form jwcid="@Form" listener="listener:formSubmit">
 Gender: <span jwcid="@PropertySelection" model="ognl:@com.myexample.DetailsPage@GENDER_MODEL" value="ognl:gender"/>
 <input type="submit"/>
</form>
public abstract class DetailsPage extends BasePage {
  public static final IPropertySelectionModel GENDER_MODEL =
      new StringPropertySelectionModel(new String[] { "Unspecified", "Female", "Male" });

  public abstract String getGender();

  public void formSubmit(IRequestCycle cycle) {
      // Process form submission
      String genderSelection = getGender();
      ...
  }
}

Example 2

Provides list of clothing items for the user to select. When the user selects a cloting item from the list the description the label and price is automatically updated. The list of clothing items would typically be loaded from a database.

PurchasePage.html

<body jwcid="@Body">
<form jwcid="@Form" listener="listener:formSubmit">
 <span jwcid="@FieldLabel" field="component:itemSelection">Selection</span>
 <span jwcid="itemSelection@PropertySelection"
       model="ognl:itemSelectionModel"
       value="ognl:clothingItem"
       onchange="javascript:this.form.events.refresh();"
       displayName="Choose an item"/>
 <input type="submit" value="Show me this item"/>
 <span jwcid="@Conditional" condition="ognl:clothingItem!=null">
 <p>Description: <span jwcid="@Insert" value="ognl:clothingItem.description"/></p>
 <p>Label: <span jwcid="@Insert" value="ognl:clothingItem.label"/></p>
 <p>Price: $<span jwcid="@Insert" value="ognl:clothingItem.price"/></p>
 </span>
</form>
</body>

PurchasePage.java

public abstract class PurchasePage extends BasePage implements PageDetachListener {

  private ItemSelectionModel model = null;

  public abstract Item getClothingItem();
  public abstract void setClothingItem(Item value);

  public ItemSelectionModel getItemSelectionModel() {
    if (model == null) {
      List items = new ArrayList();
      items.add(new Item(1, "Dress", "Cotton full length Summer dress", "CountryClub", "89.95"));
      items.add(new Item(2, "Jacket", "Gorgeous jacket", "CountryClub", "119.95"));
      model = new ItemSelectionModel(items);
    }
    return model;
  }

  public void formSubmit() {
    // Process form submission

  }

  public void pageDetached(PageEvent pageEvent) {
    model = null;
  }

}

Item.java

public class Item implements Serializable {
  private int id;
  private String name;
  private String description;
  private String label;
  private String price;

  public Item(int id, String name, String desc, String label, String price) {
      this.id = id;
      this.name = name;
      this.description = desc;
      this.label = label;
      this.price = price;
  }

  public int getId() { return id;  }

  public String getName() { return name;  }

  public String getDescription() { return description;  }

  public String getLabel() { return label;  }

  public String getPrice() { return price;  }
}

ItemSelectionModel.java

public class ItemSelectionModel implements IPropertySelectionModel, Serializable {
  private List itemList;

  public ItemSelectionModel(List itemList) {
      this.itemList = itemList;
  }

  public int getOptionCount() { return itemList.size(); }

  public Object getOption(int index) {
      return itemList.get(index);
  }

  public String getLabel(int index) {
      return ((Item) itemList.get(index)).getName();
  }

  public String getValue(int index) { return Integer.toString(index); }

  public Object translateValue(String value) {
      return getOption(Integer.parseInt(value));
  }
}