当前位置: 代码迷 >> Web前端 >> JavaEE5学习札记07-Web层与EJB集成总结(3):JSF与EJB集成
  详细解决方案

JavaEE5学习札记07-Web层与EJB集成总结(3):JSF与EJB集成

热度:446   发布时间:2012-12-25 16:18:28.0
JavaEE5学习笔记07-Web层与EJB集成总结(3):JSF与EJB集成

?JSF+EJB+JBoss集成Demo

上面提到过JSF也可以像Servet一样使用容器注入的EJB组件,那我们验证一下。我们使用同样的SessionBean作为JSF托管Bean的属性。

JSF托管Bean如下

package jsfMangerBean;

?

import javax.ejb.EJB;

import ejb.messageDrivenBean.dto.UserDTO;

import ejb.sessionBean.UserService;

?

public class TestAction {

?

??? /**

??? ?* 注入其他EJB组件

??? ?*/

??? @EJB(beanName = "UserServiceEAOImpl")

??? private UserService userService;

?

??? private UserDTO userDTO;

?

??? public UserDTO getUserDTO() {

?????? return userDTO;

??? }

?

??? public void setUserDTO(UserDTO userDTO) {

?????? this.userDTO = userDTO;

??? }

???

??? /**

??? ?* 执行插入操作

??? ?* @return

??? ?*/

??? public String insert() {

?????? userService.insertUser(userDTO);

?????? return "success";

??? }

}

servlet的功能差不多,都是插入数据。

下面是JSF页面文件

<%@ page language="java" pageEncoding="utf-8"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

??? <head>

??? </head>

??? <body>

?????? <f:view>

?????????? <h:form id="guessForm">

????????????? 主键:<h:inputText value="#{userDTO.id}">

????????????? </h:inputText>

????????????? <br />

????????????? 姓名:<h:inputText value="#{userDTO.name}">

????????????? </h:inputText>

????????????? <br />

????????????? <h:commandButton id="sumbit" action="#{testAction.insert}"

????????????????? value="提交数字" />

????????????? <br />

?????????? </h:form>

?????? </f:view>

??? </body>

</html>

JSF托管Bean以及导航配置文件内容如下faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"

??? xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

??? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

?

??? <!--托管Bean-->

??? <managed-bean>

?????? <managed-bean-name>userDTO</managed-bean-name>?? <managed-bean-class>ejb.messageDrivenBean.dto.UserDTO</managed-bean-class>

?????? <managed-bean-scope>session</managed-bean-scope>

??? </managed-bean>

?

??? <managed-bean>

?????? <managed-bean-name>testAction</managed-bean-name>??? <managed-bean-class>jsfMangerBean.TestAction</managed-bean-class>

?????? <managed-bean-scope>session</managed-bean-scope>

?????? <managed-property>

?????????? <property-name>userDTO</property-name>

?????????? <value>#{userDTO}</value>

?????? </managed-property>

??? </managed-bean>

?

??? <!--导航规则-->

??? <navigation-rule>

?????? <from-view-id>/insertUserForm.jsp</from-view-id>

?????? <navigation-case>

?????????? <from-action>#{testAction.insert}</from-action>

?????????? <from-outcome>success</from-outcome>

?????????? <to-view-id>/insertUserFormList.jsp</to-view-id>

?????? </navigation-case>

??? </navigation-rule>

??? <navigation-rule>

?????? <from-view-id>/insertUserFormList.jsp</from-view-id>

??? </navigation-rule>

</faces-config>

可视化插件对以上导航的图元化显示如下

?运行之后效果如下:

?提交之后数据库效果如下

?结果就是JSF也可以轻而易举的获得容器注入的EJB组件。