JSF MBean详解
首先从Model1中的JavaBean说起,大家知道,Model1的基本架构就是JSP页面通过JavaBean访问数据层,那么JavaBean到底是什么东东呢?
其实JavaBean只是一个特别的Java类而已。在JavaBean中,对私有的域用getXXX和setXXX来实现存取,并且必须有一个无参数的构造函数,如下类就是一个JavaBean文件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
需注意其中的格式要求,域名为第一个单词首字母小写的(usernName),对应一个域,分别有一个getUserName和setUserName两个方法,来读取和设置这一域,方法名为get/set + 域名(首字母大写)。
做过Model1开发的人会知道,JavaBean的引入方式可以是使用标签<jsp:useBean>来引入的,不需开发人员实例化即可产生一个该类的实例,因为实例是由Web容器产生的,所以我们必须提供无参数的构造函数由Web容器来调用。
再来解释JSF中的MBean,其实MBean就是一个JavaBean,因此它有着和JavaBean相同的要求。
最后是MBean的配置,JSF的MBean需要在faces-config文件中做配置,配置方法如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
对其中的managed-bean-name,managed-bean-class和managed-bean-scope做一下解释。
managed-bean-name是这一MBean的名字,用于其它位置的调用。
managed-bean-class是这一MBean的完整路径,用于指明该MBean的类文件位置。
managed-bean-scope是这一MBean的有效范围。
下面再对manage-bean-scope进行一下详细的解释,其有效取值为:application, session, request, none。很容易理解,它们的存活周期分别如下:
Name
|
Scope
|
Application
|
整个应用
|
Session
|
整个对话
|
Request
|
整个请求
|
None
|
需要时,临时
|
大家知道,JSF是以JSP为基础的,那么,对于JSP的九大对象来说,这四种scope的信息到底是怎么存储的呢?
经过测试证明,scope为applicatoin的MBean的实例保存在ServletContext中,也就是JSP中的application中,因此我们可以用如下的方法得到某个类的引用:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
对session级别的MBean,我们可以用如下方法得到其引用:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
当然,我们也可以用其它的方法得到session对象后,从session中取出实例。
对request级别的MBean,我们可从request对象中取得,代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
至于none类型的MBean,应该只能得到新的实例了。
当然,JSF提供了另外的访问MBean的方法,我们可以用如下的代码得到MBean的实例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
也可用如下的代碼直接得到MBean的一個屬性:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
就写这么多吧,累了。