jsf in action 笔记:通过值绑定初始化Bean(Setting values with value-binding expressions)--2008.04.15

2008.04.15 摘自  JSF in action  3.3.3 Setting values with value-binding expressions  Page/159

1.  JSF 中可以通过使用JSF 的EL(Expression Language)语言来引用之前创建的Managed Bean来初始化当前Managed Bean的属性,EL表达式放在#{...}中,具体的语法同组件上的EL表达式一样,可以参考之前的<jsf in action 笔记:页面语言(Expression Language)--2008.04.09  >,这种值绑定(value-binding)的方式与Spring中的依赖注入(Dependence Injection)模式有相同之处.以下为.xml配置文件中的示例代码:以下为示例图:

< managed-bean >
< managed-bean-name > defaultFavoriteSites </ managed-bean-name >
< managed-bean-class > java.util.ArrayList </ managed-bean-class >
< managed-bean-scope > none </ managed-bean-scope > 注: 没有放入到任何的scope中 < list-entries >
< value > http://www.jsfcentral.com </ value >
< value > http://www.theserverside.com </ value >
< value > http://www.ibm.com/developerworks/ </ value >
< value > http://otn.oracle.com </ value >
< value > http://www.java.net </ value >
< value > http://www.manning.com </ value >
</ list-entries >
</ managed-bean >
< managed-bean >
< description > Default user object. </ description >
< managed-bean-name > newUser </ managed-bean-name >
< managed-bean-class > org.jia.examples.UserBean </ managed-bean-class >
< managed-bean-scope > session </ managed-bean-scope > 注:存放在session的scope中
< managed-property >
< property-name > favoriteSites </ property-name >
< value > #{defaultFavoriteSites} </ value > 注:引用value binding的方式引用的defaultFavoriteSites这个上面创建的Managed Bean
</ managed-property >
< managed-property >
< property-name > favoriteAnimal </ property-name >
< value > donkey </ value >
</ managed-property >
</ managed-bean >
< managed-bean >
< managed-bean-name > exampleForm </ managed-bean-name >
< managed-bean-class > org.jia.examples.TestForm </ managed-bean-class >
< managed-bean-scope > request </ managed-bean-scope > 注:存放在request的scope中
< managed-property >
< property-name > user </ property-name >
< value > #{newUser} </ value > 注:采用value binding 引用newUser Managed Bean
</ managed-property >
</ managed-bean >

exampleForm对象中的user属性指向的是存放在session中的用户newUser,newUser的favoriteSites属性是之前创建的defaultFavoriteSites这个对象,defaultFavoriteSites这个对象不存在于任何一个scope(application,session,request)中,它的值被设定为none,(原文: Because defaultFavoriteSites is declared in the scope none, it isn’t stored anywhere—it’s created solely for the purposes of setting newUser’s favoriteSites property.)它只为这个newUser单独创建,?:就是不知道是不是为每个用户都创建一次.下面为以上代码的示例图:

JSF Setting values with value-binding expressions

2.  采用JSF EL初始化Bean的属性有几条规则:

  • bean中的属性指的对象的生存跨度(scope值)不应该比bean本身的生存跨度(scope值)短,应该符合下列表中的规定:

    An object stored in this scope…

    Can reference an object stored in this scope…

    none

    none

    application

    none, application

    session

    none, application, session

    request

    none, application, session, request

    ,就像上面的示例代码中exampleForm references newUser,This is okay, because newUser is stored in the session and exampleForm is stored in the request. newUser的生存跨度要比exampleForm的长,因为当一次请求及响应结束后,exampleForm就会被清除了,但是newUser还在,而且活的很开心(When the request is completed and exampleForm is removed, newUser will continue to live happily.),用户还会继续其它的一些操作.但如果newUser中存放了exampleForm这个对象,当newUser想去对exampleForm进行操作的时候,exampleForm可能已经被清除了,所以这样是不允许的.
  • JSF 也不支持环状,即A中引用B,B中引用A.
  • JSF的值绑定配置给我们配置对象的初始值提供了很大的便利,可以在不修改代码的条件下修改其初始值.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值