1 javax.faces.el.ValueBinding类表示一个对象,可以用它来访问由一个动作或值引用表达式所表示的属性。可调用javax.faces.application.Application类的getValueBinding方法,同时传入值引用,以此来获取一个ValueBinding的实例。
2
3 ValueBinding类有4个方法:getType、getValue、setValue和isReadOnly。我们在以下的章节中讨论这些方法。
4
5 3.6.1 获取ValueBinding所表示的对象的类型
6 调用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