1 javax.faces.el.ValueBinding類表示一個對象,可以用它來訪問由一個動作或值引用表達式所表示的屬性。可調(diào)用javax.faces.application.Application類的getValueBinding方法,同時傳入值引用,以此來獲取一個ValueBinding的實例。
2
3 ValueBinding類有4個方法:getType、getValue、setValue和isReadOnly。我們在以下的章節(jié)中討論這些方法。
4
5 3.6.1 獲取ValueBinding所表示的對象的類型
6 調(diào)用Application類的getValueBinding方法需要傳入一個值引用,返回值可能是一個JavaBean、一個JavaBean的屬性或是另外一個對象。getType方法可返回ValueBinding所表示的對象的類型。下面是getType方法的簽名:
7
8 public abstract Class getType(FacesContext facesContext)
9
10 throws javax.faces.el.PropertyNotFoundExcetpion;
11
12 比如,假設您的應用配置文件里有如下managed-bean標記:
13
14
15
16 shoppingCartBean
17
18 class>
19
20 buydirect.ShoppingCartBean
21
22 class>
23
24 session
25
26
27
28 則使用以下代碼會在控制臺輸出表示ShoppingCartBean的類名:buydirect.ShoppingCartBean。
29
30 FacesContext facesContext = FacesContext.getCurrentInstance();
31
32 ApplicationFactory factory = (ApplicationFactory)
33
34 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
35
36 Application application = factory.getApplication();
37
38 ValueBinding valueBinding =
39
40 application.getValueBinding("shoppingCartBean");
41
42 System.out.println(valueBinding.getType(facesContext).getName());
43
44 3.6.2 獲取和設置ValueBinding對象的屬性
45 要獲取由ValueBinding對象表示的屬性值,可使用getValue方法。此方法具有如下簽名:
46
47 public abstract Object getValue(FacesContext facesContext)
48
49 throws javax.faces.el.PropertyNotFoundException
50
51 比如,下面的代碼提取由一個ValueBinding表示的ShoppingCartBean對象:
52
53 FacesContext facesContext = FacesContext.getCurrentInstance();
54
55 ApplicationFactory factory = (ApplicationFactory)
56
57 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
58
59 Application application = factory.getApplication();
60
61 ValueBinding valueBinding =
62
63 application.getValueBinding("shoppingCartBean");
64
65 ShoppingCartBean bean =
66
67 (ShoppingCartBean) valueBinding.getValue(facesContext);
68
69 setValue方法則是用來改變由ValueBinding對象表示的屬性值。其方法簽名如下:
70
71 public abstract void setValue
72
73 (FacesContext facesContext, Object value)
74
75 throws javax.faces.el.PropertyNotFoundException
76
77 比如,假設ShoppingCartBean有一個叫作purchaseId的屬性,其聲明如下:
78
79 private String purchaseId;
80
81 public String getPurchaseId() {
82
83 return purchaseId;
84
85 }
86
87 public void setPurchaseId(String purchaseId) {
88
89 this.purchaseId = purchaseId;
90
91 }
92
93 則下面的代碼可獲取ShoppingCartBean對象的purchaseId屬性,并將其值設為12345。
94
95 FacesContext facesContext = FacesContext.getCurrentInstance();
96
97 ApplicationFactory factory = (ApplicationFactory)
98
99 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
100
101 Application application = factory.getApplication();
102
103 ValueBinding valueBinding =
104
105 application.getValueBinding("ShoppingCartBean.purchaseId");
106
107 valueBinding.setValue(facesContext, "12345");
108
109 // print the current value
110
111 System.out.println("Purchase Id:" +
112
113 valueBinding.getValue(facesContext));
114
115 這段代碼會在控制臺輸出如下文本:
116
117 Purchase Id: 12345
118
119 3.6.3 檢查ValueBinding屬性是否可寫
120 isReadOnly方法返回一個指示當前ValueBinding對象所表示的屬性是否可寫的boolean值。其方法簽名如下:
121
122 public abstract boolean isReadOnly(FacesContext facesContext)
123
124 throws javax.faces.el.PropertyNotFoundException
125
126 比如,下面的代碼在改變ShoppingCartBean的purchaseId屬性前先檢查其是否可寫。
127
128 FacesContext facesContext = FacesContext.getCurrentInstance();
129
130 ApplicationFactory factory = (ApplicationFactory)
131
132 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
133
134 Application application = factory.getApplication();
135
136 ValueBinding valueBinding =
137
138 application.getValueBinding("shoppingCartBean.purchaseId");
139
140 if (!valueBinding.isReadOnly(facesContext))
141
142 valueBinding.setValue(facesContext, "12345");
143
2
3 ValueBinding類有4個方法:getType、getValue、setValue和isReadOnly。我們在以下的章節(jié)中討論這些方法。
4
5 3.6.1 獲取ValueBinding所表示的對象的類型
6 調(diào)用Application類的getValueBinding方法需要傳入一個值引用,返回值可能是一個JavaBean、一個JavaBean的屬性或是另外一個對象。getType方法可返回ValueBinding所表示的對象的類型。下面是getType方法的簽名:
7
8 public abstract Class getType(FacesContext facesContext)
9
10 throws javax.faces.el.PropertyNotFoundExcetpion;
11
12 比如,假設您的應用配置文件里有如下managed-bean標記:
13
14
15
16 shoppingCartBean
17
18 class>
19
20 buydirect.ShoppingCartBean
21
22 class>
23
24 session
25
26
27
28 則使用以下代碼會在控制臺輸出表示ShoppingCartBean的類名:buydirect.ShoppingCartBean。
29
30 FacesContext facesContext = FacesContext.getCurrentInstance();
31
32 ApplicationFactory factory = (ApplicationFactory)
33
34 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
35
36 Application application = factory.getApplication();
37
38 ValueBinding valueBinding =
39
40 application.getValueBinding("shoppingCartBean");
41
42 System.out.println(valueBinding.getType(facesContext).getName());
43
44 3.6.2 獲取和設置ValueBinding對象的屬性
45 要獲取由ValueBinding對象表示的屬性值,可使用getValue方法。此方法具有如下簽名:
46
47 public abstract Object getValue(FacesContext facesContext)
48
49 throws javax.faces.el.PropertyNotFoundException
50
51 比如,下面的代碼提取由一個ValueBinding表示的ShoppingCartBean對象:
52
53 FacesContext facesContext = FacesContext.getCurrentInstance();
54
55 ApplicationFactory factory = (ApplicationFactory)
56
57 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
58
59 Application application = factory.getApplication();
60
61 ValueBinding valueBinding =
62
63 application.getValueBinding("shoppingCartBean");
64
65 ShoppingCartBean bean =
66
67 (ShoppingCartBean) valueBinding.getValue(facesContext);
68
69 setValue方法則是用來改變由ValueBinding對象表示的屬性值。其方法簽名如下:
70
71 public abstract void setValue
72
73 (FacesContext facesContext, Object value)
74
75 throws javax.faces.el.PropertyNotFoundException
76
77 比如,假設ShoppingCartBean有一個叫作purchaseId的屬性,其聲明如下:
78
79 private String purchaseId;
80
81 public String getPurchaseId() {
82
83 return purchaseId;
84
85 }
86
87 public void setPurchaseId(String purchaseId) {
88
89 this.purchaseId = purchaseId;
90
91 }
92
93 則下面的代碼可獲取ShoppingCartBean對象的purchaseId屬性,并將其值設為12345。
94
95 FacesContext facesContext = FacesContext.getCurrentInstance();
96
97 ApplicationFactory factory = (ApplicationFactory)
98
99 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
100
101 Application application = factory.getApplication();
102
103 ValueBinding valueBinding =
104
105 application.getValueBinding("ShoppingCartBean.purchaseId");
106
107 valueBinding.setValue(facesContext, "12345");
108
109 // print the current value
110
111 System.out.println("Purchase Id:" +
112
113 valueBinding.getValue(facesContext));
114
115 這段代碼會在控制臺輸出如下文本:
116
117 Purchase Id: 12345
118
119 3.6.3 檢查ValueBinding屬性是否可寫
120 isReadOnly方法返回一個指示當前ValueBinding對象所表示的屬性是否可寫的boolean值。其方法簽名如下:
121
122 public abstract boolean isReadOnly(FacesContext facesContext)
123
124 throws javax.faces.el.PropertyNotFoundException
125
126 比如,下面的代碼在改變ShoppingCartBean的purchaseId屬性前先檢查其是否可寫。
127
128 FacesContext facesContext = FacesContext.getCurrentInstance();
129
130 ApplicationFactory factory = (ApplicationFactory)
131
132 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
133
134 Application application = factory.getApplication();
135
136 ValueBinding valueBinding =
137
138 application.getValueBinding("shoppingCartBean.purchaseId");
139
140 if (!valueBinding.isReadOnly(facesContext))
141
142 valueBinding.setValue(facesContext, "12345");
143