You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Joel Wiegman <Jo...@btservices.com> on 2007/05/04 14:41:00 UTC

T5: SelectModel - a real world example

Not to be harsh, but I don't think I've ever written a select box with
constant values (Enum).  Seems like even Male/Female drop downs need to
be data-driven now-a-days.  :->

I've started to stub out a very simple "real world example" of a select
component in T5, but it doesn't appear to be as simple as I thought it
would be.  

A shiny nickel to anyone that can spot the flaw, because I sure can't...
I'm new to Tapestry in general so I could be missing something really
mundane here, but seems like it should work in my mind.

Here are the players:

<< THE ERROR >>

[ERROR] DefaultRequestExceptionHandler Processing of request failed with
uncaught exception: com.foo.data.Brand cannot be cast to
java.lang.String
java.lang.ClassCastException: com.foo.data.Brand cannot be cast to
java.lang.String
        at
org.apache.tapestry.corelib.components.Select$1.toClient(Select.java:62)
        at
org.apache.tapestry.corelib.components.Select.writeOptions(Select.java:1
94)
        at
org.apache.tapestry.corelib.components.Select.options(Select.java:169)
        at
org.apache.tapestry.corelib.components.Select.beforeRenderTemplate(Selec
t.java)


<< Main.html (abridged) >>

<t:form>
	<select t:type="select" t:id="brand" value="brand"
model="brandSelectModel"/>
</t:form>


<< Main.java (abridged) >>

package com.foo.pages;

import com.foo.data.Brand;

public class Main {

	private Brand brand;

	public Brand getBrand() {
		return brand;
	}
	
	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public BrandSelectModel getBrandSelectModel() {
		return new BrandSelectModel(getBrands());
	}

	public List<Brand> getBrands() {
		List<Brand> brands = new ArrayList<Brand>();
		brands.add(new Brand("1", "Brand 1"));
		brands.add(new Brand("2", "Brand 2"));		
		brands.add(new Brand("3", "Brand 3"));
		return brands;
	}

}


<< Brand.java (abridged) >>

package com.foo.data;

public class Brand {

	private String id;
	private String description;

	public Brand() {}

	public Brand(String id, String description) {
		this.id = id;
		this.description = description;
	}

	public String getId() {
		return id;
	}

	public String getDescription() {
		return description;
	}

}


<< BrandSelectModel.java >>

package com.foo.uisupport;

import com.foo.data.Brand;

public class BrandSelectModel implements SelectModel {
	
	private List<Brand> brands;
	
	public BrandSelectModel(List<Brand> brands) {
		this.brands = brands;
	}
	
	public List<OptionGroupModel> getOptionGroups() {
		return null;
	}

	public List<OptionModel> getOptions() {
		List<OptionModel> optionModelList = new
ArrayList<OptionModel>();
		for(Brand brand: brands) {
			optionModelList.add(new
OptionModelImpl(brand.getDescription(), false, brand, new String[0]));
		}
		return optionModelList;
	}

}



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5: SelectModel - a real world example

Posted by Howard Lewis Ship <hl...@gmail.com>.
In your OptionModel, the label is the Brand description, and the value is
the Brand itself.

You then supply a ValueEncoder that converts between Brands and brand ids
(as strings, for the client side). If Brand is an entity object, then it may
be necessary to have the ValueEncoder talk to the database or session store.

On 5/4/07, Joel Wiegman <Jo...@btservices.com> wrote:
>
> Thanks for the reply Howard.
>
> >> The Select component doesn't know how to create a client-side
> representation of a Brand (it doesn't magically know to use the id).
>
> So, then... I guess my question would be... what is the BrandSelectModel
> for?  In that object, I'm essentially mapping a Brand's "description" to
> the Brand object.
>
> Just curious what your thoughts are on that...
>
>
> -----Original Message-----
> From: Howard Lewis Ship [mailto:hlship@gmail.com]
> Sent: Friday, May 04, 2007 1:42 PM
> To: Tapestry users
> Subject: Re: T5: SelectModel - a real world example
>
> In the simplest case, T5 thinks that the options in the drop down list
> are all strings.
>
> In your case, they are Brands.  The Select component doesn't know how to
> create a client-side representation of a Brand (it doesn't magically
> know to use the id).
>
> You must provide a ValueEncoder that can convert between Brands and
> client-side string values.  This is the encoder parameter of the Select
> component.
>
>
> On 5/4/07, Joel Wiegman <Jo...@btservices.com> wrote:
> >
> > Not to be harsh, but I don't think I've ever written a select box with
>
> > constant values (Enum).  Seems like even Male/Female drop downs need
> > to be data-driven now-a-days.  :->
> >
> > I've started to stub out a very simple "real world example" of a
> > select component in T5, but it doesn't appear to be as simple as I
> > thought it would be.
> >
> > A shiny nickel to anyone that can spot the flaw, because I sure
> can't...
> > I'm new to Tapestry in general so I could be missing something really
> > mundane here, but seems like it should work in my mind.
> >
> > Here are the players:
> >
> > << THE ERROR >>
> >
> > [ERROR] DefaultRequestExceptionHandler Processing of request failed
> > with uncaught exception: com.foo.data.Brand cannot be cast to
> > java.lang.String
> > java.lang.ClassCastException: com.foo.data.Brand cannot be cast to
> > java.lang.String
> >         at
> >
> org.apache.tapestry.corelib.components.Select$1.toClient(Select.java:62)
> >         at
> > org.apache.tapestry.corelib.components.Select.writeOptions(Select.java
> > :1
> > 94)
> >         at
> > org.apache.tapestry.corelib.components.Select.options(Select.java:169)
> >         at
> > org.apache.tapestry.corelib.components.Select.beforeRenderTemplate(Sel
> > ec
> > t.java)
> >
> >
> > << Main.html (abridged) >>
> >
> > <t:form>
> >         <select t:type="select" t:id="brand" value="brand"
> > model="brandSelectModel"/>
> > </t:form>
> >
> >
> > << Main.java (abridged) >>
> >
> > package com.foo.pages;
> >
> > import com.foo.data.Brand;
> >
> > public class Main {
> >
> >         private Brand brand;
> >
> >         public Brand getBrand() {
> >                 return brand;
> >         }
> >
> >         public void setBrand(Brand brand) {
> >                 this.brand = brand;
> >         }
> >
> >         public BrandSelectModel getBrandSelectModel() {
> >                 return new BrandSelectModel(getBrands());
> >         }
> >
> >         public List<Brand> getBrands() {
> >                 List<Brand> brands = new ArrayList<Brand>();
> >                 brands.add(new Brand("1", "Brand 1"));
> >                 brands.add(new Brand("2", "Brand 2"));
> >                 brands.add(new Brand("3", "Brand 3"));
> >                 return brands;
> >         }
> >
> > }
> >
> >
> > << Brand.java (abridged) >>
> >
> > package com.foo.data;
> >
> > public class Brand {
> >
> >         private String id;
> >         private String description;
> >
> >         public Brand() {}
> >
> >         public Brand(String id, String description) {
> >                 this.id = id;
> >                 this.description = description;
> >         }
> >
> >         public String getId() {
> >                 return id;
> >         }
> >
> >         public String getDescription() {
> >                 return description;
> >         }
> >
> > }
> >
> >
> > << BrandSelectModel.java >>
> >
> > package com.foo.uisupport;
> >
> > import com.foo.data.Brand;
> >
> > public class BrandSelectModel implements SelectModel {
> >
> >         private List<Brand> brands;
> >
> >         public BrandSelectModel(List<Brand> brands) {
> >                 this.brands = brands;
> >         }
> >
> >         public List<OptionGroupModel> getOptionGroups() {
> >                 return null;
> >         }
> >
> >         public List<OptionModel> getOptions() {
> >                 List<OptionModel> optionModelList = new
> > ArrayList<OptionModel>();
> >                 for(Brand brand: brands) {
> >                         optionModelList.add(new
> > OptionModelImpl(brand.getDescription(), false, brand, new String[0]));
> >                 }
> >                 return optionModelList;
> >         }
> >
> > }
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>
>
> --
> Howard M. Lewis Ship
> TWD Consulting, Inc.
> Independent J2EE / Open-Source Java Consultant Creator and PMC Chair,
> Apache Tapestry Creator, Apache HiveMind
>
> Professional Tapestry training, mentoring, support and project work.
> http://howardlewisship.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

RE: T5: SelectModel - a real world example

Posted by Joel Wiegman <Jo...@btservices.com>.
Thanks for the reply Howard.

>> The Select component doesn't know how to create a client-side
representation of a Brand (it doesn't magically know to use the id).

So, then... I guess my question would be... what is the BrandSelectModel
for?  In that object, I'm essentially mapping a Brand's "description" to
the Brand object.

Just curious what your thoughts are on that...


-----Original Message-----
From: Howard Lewis Ship [mailto:hlship@gmail.com] 
Sent: Friday, May 04, 2007 1:42 PM
To: Tapestry users
Subject: Re: T5: SelectModel - a real world example

In the simplest case, T5 thinks that the options in the drop down list
are all strings.

In your case, they are Brands.  The Select component doesn't know how to
create a client-side representation of a Brand (it doesn't magically
know to use the id).

You must provide a ValueEncoder that can convert between Brands and
client-side string values.  This is the encoder parameter of the Select
component.


On 5/4/07, Joel Wiegman <Jo...@btservices.com> wrote:
>
> Not to be harsh, but I don't think I've ever written a select box with

> constant values (Enum).  Seems like even Male/Female drop downs need 
> to be data-driven now-a-days.  :->
>
> I've started to stub out a very simple "real world example" of a 
> select component in T5, but it doesn't appear to be as simple as I 
> thought it would be.
>
> A shiny nickel to anyone that can spot the flaw, because I sure
can't...
> I'm new to Tapestry in general so I could be missing something really 
> mundane here, but seems like it should work in my mind.
>
> Here are the players:
>
> << THE ERROR >>
>
> [ERROR] DefaultRequestExceptionHandler Processing of request failed 
> with uncaught exception: com.foo.data.Brand cannot be cast to 
> java.lang.String
> java.lang.ClassCastException: com.foo.data.Brand cannot be cast to 
> java.lang.String
>         at
>
org.apache.tapestry.corelib.components.Select$1.toClient(Select.java:62)
>         at
> org.apache.tapestry.corelib.components.Select.writeOptions(Select.java
> :1
> 94)
>         at
> org.apache.tapestry.corelib.components.Select.options(Select.java:169)
>         at
> org.apache.tapestry.corelib.components.Select.beforeRenderTemplate(Sel
> ec
> t.java)
>
>
> << Main.html (abridged) >>
>
> <t:form>
>         <select t:type="select" t:id="brand" value="brand"
> model="brandSelectModel"/>
> </t:form>
>
>
> << Main.java (abridged) >>
>
> package com.foo.pages;
>
> import com.foo.data.Brand;
>
> public class Main {
>
>         private Brand brand;
>
>         public Brand getBrand() {
>                 return brand;
>         }
>
>         public void setBrand(Brand brand) {
>                 this.brand = brand;
>         }
>
>         public BrandSelectModel getBrandSelectModel() {
>                 return new BrandSelectModel(getBrands());
>         }
>
>         public List<Brand> getBrands() {
>                 List<Brand> brands = new ArrayList<Brand>();
>                 brands.add(new Brand("1", "Brand 1"));
>                 brands.add(new Brand("2", "Brand 2"));
>                 brands.add(new Brand("3", "Brand 3"));
>                 return brands;
>         }
>
> }
>
>
> << Brand.java (abridged) >>
>
> package com.foo.data;
>
> public class Brand {
>
>         private String id;
>         private String description;
>
>         public Brand() {}
>
>         public Brand(String id, String description) {
>                 this.id = id;
>                 this.description = description;
>         }
>
>         public String getId() {
>                 return id;
>         }
>
>         public String getDescription() {
>                 return description;
>         }
>
> }
>
>
> << BrandSelectModel.java >>
>
> package com.foo.uisupport;
>
> import com.foo.data.Brand;
>
> public class BrandSelectModel implements SelectModel {
>
>         private List<Brand> brands;
>
>         public BrandSelectModel(List<Brand> brands) {
>                 this.brands = brands;
>         }
>
>         public List<OptionGroupModel> getOptionGroups() {
>                 return null;
>         }
>
>         public List<OptionModel> getOptions() {
>                 List<OptionModel> optionModelList = new 
> ArrayList<OptionModel>();
>                 for(Brand brand: brands) {
>                         optionModelList.add(new 
> OptionModelImpl(brand.getDescription(), false, brand, new String[0]));
>                 }
>                 return optionModelList;
>         }
>
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


--
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant Creator and PMC Chair,
Apache Tapestry Creator, Apache HiveMind

Professional Tapestry training, mentoring, support and project work.
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5: SelectModel - a real world example

Posted by Howard Lewis Ship <hl...@gmail.com>.
In the simplest case, T5 thinks that the options in the drop down list are
all strings.

In your case, they are Brands.  The Select component doesn't know how to
create a client-side representation of a Brand (it doesn't magically know to
use the id).

You must provide a ValueEncoder that can convert between Brands and
client-side string values.  This is the encoder parameter of the Select
component.


On 5/4/07, Joel Wiegman <Jo...@btservices.com> wrote:
>
> Not to be harsh, but I don't think I've ever written a select box with
> constant values (Enum).  Seems like even Male/Female drop downs need to
> be data-driven now-a-days.  :->
>
> I've started to stub out a very simple "real world example" of a select
> component in T5, but it doesn't appear to be as simple as I thought it
> would be.
>
> A shiny nickel to anyone that can spot the flaw, because I sure can't...
> I'm new to Tapestry in general so I could be missing something really
> mundane here, but seems like it should work in my mind.
>
> Here are the players:
>
> << THE ERROR >>
>
> [ERROR] DefaultRequestExceptionHandler Processing of request failed with
> uncaught exception: com.foo.data.Brand cannot be cast to
> java.lang.String
> java.lang.ClassCastException: com.foo.data.Brand cannot be cast to
> java.lang.String
>         at
> org.apache.tapestry.corelib.components.Select$1.toClient(Select.java:62)
>         at
> org.apache.tapestry.corelib.components.Select.writeOptions(Select.java:1
> 94)
>         at
> org.apache.tapestry.corelib.components.Select.options(Select.java:169)
>         at
> org.apache.tapestry.corelib.components.Select.beforeRenderTemplate(Selec
> t.java)
>
>
> << Main.html (abridged) >>
>
> <t:form>
>         <select t:type="select" t:id="brand" value="brand"
> model="brandSelectModel"/>
> </t:form>
>
>
> << Main.java (abridged) >>
>
> package com.foo.pages;
>
> import com.foo.data.Brand;
>
> public class Main {
>
>         private Brand brand;
>
>         public Brand getBrand() {
>                 return brand;
>         }
>
>         public void setBrand(Brand brand) {
>                 this.brand = brand;
>         }
>
>         public BrandSelectModel getBrandSelectModel() {
>                 return new BrandSelectModel(getBrands());
>         }
>
>         public List<Brand> getBrands() {
>                 List<Brand> brands = new ArrayList<Brand>();
>                 brands.add(new Brand("1", "Brand 1"));
>                 brands.add(new Brand("2", "Brand 2"));
>                 brands.add(new Brand("3", "Brand 3"));
>                 return brands;
>         }
>
> }
>
>
> << Brand.java (abridged) >>
>
> package com.foo.data;
>
> public class Brand {
>
>         private String id;
>         private String description;
>
>         public Brand() {}
>
>         public Brand(String id, String description) {
>                 this.id = id;
>                 this.description = description;
>         }
>
>         public String getId() {
>                 return id;
>         }
>
>         public String getDescription() {
>                 return description;
>         }
>
> }
>
>
> << BrandSelectModel.java >>
>
> package com.foo.uisupport;
>
> import com.foo.data.Brand;
>
> public class BrandSelectModel implements SelectModel {
>
>         private List<Brand> brands;
>
>         public BrandSelectModel(List<Brand> brands) {
>                 this.brands = brands;
>         }
>
>         public List<OptionGroupModel> getOptionGroups() {
>                 return null;
>         }
>
>         public List<OptionModel> getOptions() {
>                 List<OptionModel> optionModelList = new
> ArrayList<OptionModel>();
>                 for(Brand brand: brands) {
>                         optionModelList.add(new
> OptionModelImpl(brand.getDescription(), false, brand, new String[0]));
>                 }
>                 return optionModelList;
>         }
>
> }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com