T5: SelectModel - a real world example(转载Tapestry maillist)

java 代码
 
  1. Eureka!  
  2.   
  3. Finally got it all working.  What caused the drop down to not be  
  4. defaulting is the fact that I declared my "Brand" list in two separate  
  5. lists... One for the ValueEncoder and one for the SelectModel.  Big no  
  6. no!  An "equals" is called on the value from the SelectModel and the  
  7. value from the ValueEncoder, so if they are initialized as separate  
  8. objects in separate lists the framework won't notice that they are equal  
  9. (because they would point to a different memory location).  I found that  
  10. the framework also seems to like things better if the List of Brands is  
  11. "@Persist"ed between pages.  Here's the final source code that I used to  
  12. get it all working (Enjoy!):  
  13.   
  14. Brand.java (simple POJO):  
  15.   
  16. public class Brand {  
  17.   
  18.     private String id;  
  19.     private String description;  
  20.       
  21.     public Brand() {}  
  22.   
  23.     public Brand(String id, String description) {  
  24.         this.id = id;  
  25.         this.description = description;  
  26.     }     
  27.   
  28.     public String getId() {  
  29.         return id;  
  30.     }  
  31.   
  32.     public String getDescription() {  
  33.         return description;  
  34.     }  
  35.       
  36. }  
  37.   
  38.   
  39. Test.java  
  40.   
  41. public class Test {  
  42.   
  43.     @Persist  
  44.     private Brand brand;  
  45.   
  46.     @Persist  
  47.     private List<Brand> brands;  
  48.       
  49.     public Brand getBrand() {  
  50.         return brand;  
  51.     }  
  52.   
  53.     public void setBrand(Brand brand) {  
  54.         this.brand = brand;  
  55.     }  
  56.       
  57.     private List<Brand> getBrands()  
  58.     {  
  59.         if(brands == null) {  
  60.             brands = new ArrayList<Brand>();  
  61.             brands.add(new Brand("1""Brand 1"));  
  62.             brands.add(new Brand("2""Brand 2"));  
  63.             brands.add(new Brand("3""Brand 3"));  
  64.             brands.add(new Brand("4""Brand 4"));  
  65.         }  
  66.           
  67.         return brands;  
  68.     }  
  69.       
  70.     public BrandSelectModel getBrandSelectModel() {  
  71.         return new BrandSelectModel(getBrands());  
  72.     }  
  73.   
  74.     public BrandValueEncoder getBrandValueEncoder() {  
  75.         return new BrandValueEncoder(getBrands());  
  76.     }  
  77.       
  78.     public class BrandValueEncoder implements ValueEncoder<Brand> {  
  79.           
  80.         private List<Brand> brands;  
  81.           
  82.         public BrandValueEncoder(List<Brand> brands) {  
  83.             this.brands = brands;  
  84.         }  
  85.   
  86.         public String toClient(Brand brand) {  
  87.             return brand.getDescription();  
  88.         }  
  89.   
  90.         public Brand toValue(String string) {  
  91.             for(Brand brand : brands) {  
  92.       
  93. if(brand.getDescription().equals(string)) {  
  94.                     return brand;  
  95.                 }  
  96.             }  
  97.             return null;  
  98.         }  
  99.   
  100.     }  
  101.       
  102.     public class BrandSelectModel implements SelectModel {  
  103.           
  104.         private List<Brand> brands;  
  105.           
  106.         public BrandSelectModel(List<Brand> brands) {  
  107.             this.brands = brands;  
  108.         }  
  109.           
  110.         public List<OptionGroupModel> getOptionGroups() {  
  111.             return null;  
  112.         }  
  113.   
  114.         public List<OptionModel> getOptions() {  
  115.             List<OptionModel> optionModelList = new  
  116. ArrayList<OptionModel>();  
  117.             for(Brand brand: brands) {  
  118.                 optionModelList.add(  
  119.                     new OptionModelImpl(  
  120.                         brand.getDescription(),   
  121.                         false,   
  122.                         brand,   
  123.                         new String[0]  
  124.                     )  
  125.                 );  
  126.             }  
  127.             return optionModelList;  
  128.         }  
  129.   
  130.     }  
  131.       
  132. }  
xml 代码
 
  1. Test.html  
  2.   
  3. <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">  
  4. <t:form>  
  5.   
  6.     <select t:type="select" t:id="brand"  
  7.         value="brand" model="brandSelectModel"  
  8. encoder="brandValueEncoder"  
  9.         onchange="this.form.submit();" />  
  10.   
  11.     <br/>  
  12.       
  13.     <t:if test="brand">  
  14.         ${brand.description}  
  15.     </t:if>  
  16.   
  17. </t:form>  
  18. </html>  
<brand>
Eureka! 最终调试好的代码:BrandSelectModel 和BrandValueEncoder 写的都挺完美的,要比我写的流畅多了,把List brands 作为参数传入,很符合写Swing程序时候的习惯,如果作为全局变量就显的混乱的多。另外 toClient(Brand brand) 接口里的声明是Object 类型,在我这里会报错。真希望多一些这样的classic demo。
<brand> <brand> <brand> <brand> <brand> <brand> <brand> <brand> <optiongroupmodel> <optionmodel> <optionmodel> <optionmodel></optionmodel> </optionmodel> </optionmodel> </optiongroupmodel> </brand></brand></brand></brand></brand></brand></brand></brand></brand>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值