认识WebBean ---- 定义

    Gavin King在开发 Seam框架的过程当中,为了弥补EJB3和JSF框架当中的裂缝,便引入了Web Beans的概念,将EJB3和JSF无缝的整合了起来。

   WebBeans(JSR-299)根据 JBoss 提交的 WebBean 规范文档,WebBean定义是:
  
1. Web Bean
引用

Chapter 2. Web Bean definition
A Web Bean is a Java EE component that bears additional metadata defining its lifecycle and interactions with other components
according to the Web Beans context model.

  WebBean 是一个 JavaEE 组件,它定义了额外的元数据信息,用于控制自己的生命周期以及与WebBeans上下文中其他组件之间的交互。

  WebBean 定义了应用的状态(State)和逻辑(Logic);WebBean 以多实例方式运行。 
2. Web Bean Manager 
  Web规范中另一个重要的组件是 Web Bean Manager 。 与以往的EJB规范类似,它是一个容器组件,用来管理WebBean实例。包括:
  •   创建(create)和销毁(destroy) WebBean 实例;
  •   与适当的上下文(Context)相关联;
  •   WebBean 之间的注入;
  •   为 EL(Expression Language)等其他组件提供对 WebBean 的取值访问等。

3. WebBean 构成
每个 WebBean 构成包括以下元素:
  •   一套API (必须)
  •   一套绑定注解(Binding Annotation)
  •   范围(Scope)
  •   部署类型(Deployment Type)
  •   WebBean 名称(name,可选)
  •   拦截器绑定
  •   WebBean 实现
 
4. WebBean 例子
  以上是WebBean 规范文档中对WebBean定义的基本描述,看起来还是比较抽象。 下面我们通过一个例子看看WebBean到底长什么样子。
  JSF Example (摘自WebBean规范) 
1) 一个JSF 登录页
<f:view>
<f:form>
<h:panelGrid columns="2" rendered="#{!login.loggedIn}">
<h:outputLabel for="username">Username:</h:outputLabel>
<h:inputText id="username" value="#{credentials.username}"/>
<h:outputLabel for="password">Password:</h:outputLabel>
<h:inputText id="password" value="#{credentials.password}"/>
</h:panelGrid>
<h:commandButton value="Login" action="#{login.login}" rendered="#{!login.loggedIn}"/>
<h:commandButton value="Logout" acion="#{login.logout}" rendered="#{login.loggedIn}"/>
</f:form>
</f:view>

2)  其中,credentials 在JSF中称为 ManagedBean。在新规范中它称为一个 WebBean,仍是MVC中的Model,生命周期为request绑定,定义:
@Model
public class Credentials {
private String username;
private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}

其中Web Bean的 @Model 注解表明它是一个数据模型。

3) 其中 Login  在JSF中称为 ManagedBean。在新规范中也是一个WebBean,作为MVC中的Control,完成登录的逻辑处理。
@SessionScoped @Model
public class Login {
@Current Credentials credentials;
@PersistenceContext EntityManager userDatabase;
private User user;

public void login() {
List<User> results = userDatabase.createQuery(
"select u from User u where u.username=:username and u.password=:password")
.setParameter("username", credentials.getUserName())
.setParameter("password", credentials.getPassword())
.getResultList();
if ( !results.isEmpty() ) {
user = results.get(0);
}
}
public void logout() {
user = null;
public boolean isLoggedIn() {
return user!=null;
}
@Produces @LoggedIn User getCurrentUser() {
if (user==null) {
throw new NotLoggedInException();
}
else {
return user;
}
}
}


其中,@SessionScoped 注解标明 Login 的生命周期是 HTTP Session;
@Current 绑定注解标明需要容器将另一个WebBEan credentials 注入给 Login Bean;
@PersistenceContext 注解标明需要使用 JPA EntityManager。

  规范文档第一章还介绍了 EJB Example,Interceptor Example,Decorator Example,可以更进一一览 Web Bean 的容貌。

5. 总结
   从以上简单介绍和例子我们可以看出, WebBean 规范是对现有MVC架构及JSF等规范的一个补充和明确,将以前停留在通行的设计惯例进一步形成明确的规范,并与JSF,Seam,JEE6等Gavin King,Sun,Jboss主推的规范充分融合。 它仍然在MVC设计架构之下。

你可能感兴趣的:(bean,Web,JSF,seam,webbeans)