Over a million developers have joined DZone.

A Simpler Select for Tapestry

DZone's Guide to

A Simpler Select for Tapestry

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

After writing my last post, Howard came up with a better idea. Why to stick to only one implementation of Select ? If we need a simpler Select, why not create a simpler one. So, I came with a simple implementation!!

I have not fully tested it (something for next weekend), but it should work as it is mostly copied from the original Select component.


The usage is quite similar to the one suggested by Inge.

<select t:type='simpleSelect' t:value='user' t:items='users' t:label='name'

label can also be an expression like #{name} (#{address}). It uses '#' instead of '$' as '${}' expressions in a template are resolved by Tapestry and result is passed to the component instead of the actual expression.

How it works

@Events({ EventConstants.VALIDATE,
EventConstants.VALUE_CHANGED + " when 'zone' parameter is 'bound'" })
public class SimpleSelect extends AbstractField {
public static final String CHANGE_EVENT = "change";

@Parameter(required = true, allowNull = false)
private List<?> items;

@Parameter(defaultPrefix = BindingConstants.LITERAL, allowNull = false)
private String label;

@Parameter(defaultPrefix = BindingConstants.LITERAL, allowNull = false)
private String key;

@Parameter(autoconnect = true, required = true)
private Object value;

@Parameter(value = "auto", defaultPrefix = BindingConstants.LITERAL)
private BlankOption blankOption;

@Parameter(defaultPrefix = BindingConstants.LITERAL)
private String blankLabel;

@Parameter(defaultPrefix = BindingConstants.VALIDATE)
private FieldValidator<Object> validate;

@Parameter(defaultPrefix = BindingConstants.LITERAL)
private String zone;

private Request request;

private ComponentResources resources;

private ComponentDefaultProvider defaultProvider;

private ValidationTracker tracker;

private PropertyAccess propertyAccess;

private TypeCoercer typeCoercer;

private FieldValidationSupport fieldValidationSupport;

private JavaScriptSupport javaScriptSupport;

private RenderDisabled renderDisabled;

private Map<String, PropertyAdapter> adapterMap;

private final static Pattern PROPERTY_PATTERN = Pattern.compile("#\\{([\\w.$_]+)\\}");

protected void processSubmission(String elementName) {
String submittedValue = request.getParameter(elementName);
tracker.recordInput(this, submittedValue);

Object selectedValue = toValue(submittedValue);

try {
fieldValidationSupport.validate(selectedValue, resources, validate);
value = selectedValue;
} catch (ValidationException ex) {
tracker.recordError(this, ex.getMessage());


private Object toValue(String submittedValue) {
if (InternalUtils.isBlank(submittedValue)) {
return null;
} else {
Object submittedKey = typeCoercer.coerce(submittedValue, getPropertyType(key));

for (Object item : items) {
Object itemKey = getPropertyValue(item, key);
if (itemKey.equals(submittedKey)) {
return item;
return null;

Object onChange(@RequestParameter(value = "t:selectvalue", allowBlank = true)
final String selectValue){
final Object newValue = toValue(selectValue);
CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();
this.resources.triggerEvent(EventConstants.VALUE_CHANGED, new Object[]
{ newValue }, callback);
this.value = newValue;
return callback.getResult();

void beginRender(MarkupWriter writer) {
writer.element("select", "id", getClientId(), "name", getControlName());

if (zone != null) {
Link link = resources.createEventLink(CHANGE_EVENT);
JSONObject spec = new JSONObject("selectId", getClientId(),
"zoneId", zone, "url", link.toURI());
javaScriptSupport.addInitializerCall("linkSelectToZone", spec);

void afterRender(MarkupWriter writer) {

void options(MarkupWriter writer) {
if (showBlankOption()) {
writer.element("option", "value", "");

for (Object item : items) {
writer.element("option", "value", getPropertyValue(item, key));

if (value != null && getPropertyValue(key).equals(getPropertyValue(item, key))) {
writer.attributes("selected", "selected");

private String getLabel(Object item) {
if (label.contains("#")) {
if (adapterMap == null) {
adapterMap = new HashMap<String, PropertyAdapter>();
Matcher matcher = PROPERTY_PATTERN.matcher(label);
while (matcher.find()) {

String text = this.label;
for (String key : adapterMap.keySet()) {
String value = adapterMap.get(key) == null ? ""
: adapterMap.get(key).get(item).toString();
text = text.replace(key, value);
return text;
} else {
Object propertyValue = getPropertyValue(item, label);
if(propertyValue == null){
return "";
}else {
return propertyValue.toString();

private boolean showBlankOption() {
switch (blankOption) {
case ALWAYS:
return true;

case NEVER:
return false;

return !isRequired();

String defaultBlankLabel() {
Messages containerMessages = resources.getContainerMessages();
String key = resources.getId() + "-blanklabel";
if (containerMessages.contains(key)) {
return containerMessages.get(key);
return null;

Binding defaultValidate() {
return defaultProvider.defaultValidatorBinding("value", resources);

private Object getPropertyValue(String property) {
return getPropertyValue(value, property);

private Object getPropertyValue(Object object, String property) {
if (object == null) {
return null;
return propertyAccess.getAdapter(object).getPropertyAdapter(property).

private Class<?> getPropertyType(String property) {
return propertyAccess.getAdapter(resources.getBoundType("value")).


This is mostly the same as the original Select component but instead of using a SelectModel it uses a List and instead of using a ValueEncoder, it relies on the TypeCoercer service for convertion between the client submitted value(String) and the item’s key.

There are two important changes.

  1. Line 85-92 : In order to get the submitted value, we convert the request parameter corresponding to this component to the item’s key using TypeCoercer service and then search through the list for an item with the same key. (Remember the key value has to be unique for each item)
  2. Line 126-144: Here we add a blank option, based on the blankOption parameter and blankLabel parameter. We then add an option tag for each item. The value for each option is obtained after converting its key to String. The content of each option is the resolved label expression.


From http://tawus.wordpress.com/2011/06/06/a-simpler-select-for-tapestry/

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}