Book Image

Tapestry 5: Building Web Applications

Book Image

Tapestry 5: Building Web Applications

Overview of this book

Table of Contents (17 chapters)
Tapestry 5
Credits
About the Author
About the Reviewers
Preface
Foreword
Where to Go Next

Creating a ValueEncoder


The ValueEncoder interface has only two methods:

  • String toClient(Object obj) that tells Tapestry what exactly should be used for the value attribute of<option>, corresponding to the given object.

  • Object toValue(String str), works in the opposite direction, given the contents of the value attribute of an option selected by the user, it should define which exactly object corresponds to this value.

Here is one possible implementation of this interface that will deal with the Celebrity object in our application:

package com.packtpub.celebrities.util;
import com.packtpub.celebrities.data.IDataSource;
import com.packtpub.celebrities.model.Celebrity;
import org.apache.tapestry.ValueEncoder;
public class CelebrityEncoder implements ValueEncoder
{
private IDataSource source;
public CelebrityEncoder(IDataSource source)
{
this.source = source;
}
public String toClient(Object obj)
{
return "" + ((Celebrity)obj).getId();
}
public Object toValue(String str)
{
return source...