Managed Beans
方弘
2023-12-01
1、Managed beans的编写
1)采用注释的方式进行实例化
@ManagedBean/@ManagedBean(name="beanName")
@SessionScoped
public class className{}
2)在faces-config.xml文件进行配置
<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>完全类路径</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
2、给bean的属性注入值
1、采用注释方式注入
@ManagedBean
@SessionScoped
public class UserBean {
@ManagedProperty(value="Jane")
private String firstName;
@ManagedProperty(value="Doe")
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
2、采用xml方式注入(fancs.config.xml)<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>完全类路径</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>firstName</property-name>
<value>Jane</value>
</managed-property>
<managed-property>
<property-name>lastName</property-name>
<value>Doe</value>
</managed-property>
</managed-bean>
2.1、给list类型的属性注入值
<managed-bean>
...
<managed-property>
<property-name>sportsInterests</property-name>
<list-entries>
<value>Cycling</value>
<value>Running</value>
<value>Swimming</value>
<value>Kayaking</value>
...
</list-entries>
</managed-property>
</managed-bean>
注:</list-entries>默认的list泛型是String类型,如果要注入其他类型可以这样使用:
<managed-bean>
...
<managed-property>
<property-name>racePlacement</property-name>
<list-entries>
<value-class>java.lang.Integer</value-class>
<value>23</value>
<value>12</value>
<value>3</value>
<value>1</value>
...
</list-entries>
</managed-property>
</managed-bean>
Element Description
。。<managed-property> Same as before, the parent element for managed properties.
。。<property-name> The name of the property, which must be of type array
or java.util.List.
。。<property-class> JSF uses the default type of ArrayList, but any other list concrete
class could be specified here.
。。<list-entries>(1 to N) Parent element of the <value-class>, list values defined in multiple
<value> or <null-value> elements.
。。<value-class> When not using the default list type of java.lang.String, one must
specify the list item data type here. (Optional)
。。<value> or <null-value> The <value> element provides the value with which to set the
property. When initializing a property to a null value, a <null-value>
tag is used instead.
Note: The <null-value> element cannot be used for primitive data types.
注:list的类型可以这样读
<h:outputText value="#{userBean.sportsInterests[0]}"/>
2.2给map类型的属性注入
<managed-bean>
...
<managed-property>
<property-name>sportsInterests</property-name>
<map-entries>
<map-entry>
<key>Cycling</key>
<value>Any competitive athletic event where a bicycle is used.</value>
</map-entry>
<map-entry>
<key>Running</key>
<value>Any competitive athletic event where the competitors are running or jogging.</value>
</map-entry>
<map-entry>
<key>Swimming</key>
<value>Any competitive athletic event where the competitors are swimmming.</value>
</map-entry>
<map-entry>
<key>Kayaking</key>
<value>Any competitive athletic event where the competitors use a kayak.</value>
</map-entry>
</map-entries>
...
</managed-property>
</managed-bean>
注:map的类型可以这样读:
<h:outputText value="#{userBean.sportsInterests['Swimming']}"/>
Element Description
。。<managed-property> Same as before, the parent element for managed properties.
。。<property-name> The name of the property.
。。<property-class> JSF uses the default type of HashMap, but any other Map concrete
class could be specified here.
。。<map-entries> Parent element of <key-class>, <value-class>, and Map entries.
。。<key-class> Element specifying data type of the keys used in the map. When
not specified, a java.lang.String is used as a default. (Optional)
。。<value-class> Element specifying data type of the values used in the map.
When not specified, a java.lang.String is also used as a default.(Optional)
。。<map-entry>(1 to N) Parent element to a Map key/value element pair.
。。<key> The key value used to look up an associated value.
。。<value>or<null-value> The <value> element that is retrieved when the associated key
is supplied. To initialize a Map property to a null value, a <nullvalue> tag is used.
Note: The <null-value> element cannot be used for primitive data types.
3、Controlling Managed Bean Life Spans
#{userBean }
#{userBean }
#{userBean }
#{userBean }
1)@NoneScoped
像这样写在同一个页面中,只有一个请求,但是会实例化四次bean。
2)@RequestScoped
实例化一次,每次请求得到的对象都是不同的。即请求一次实例化一次。
3)@ViewScoped
在同一页面内是同一对象的,但通过浏览器去刷新,每次都会实例化。
4)@SessionScoped
一次会话内都是同一对象,除非session失效或者超时。
5)@ApplicationScoped
存活于整个应用服务。