zhangrong108 发表于 2013-2-3 10:16:17

java Tapestry5 <select>

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
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>
页: [1]
查看完整版本: java Tapestry5 <select>