JSF Backing Bean(EJB)未从JSF调用(使用glassfish)

时间:2014-01-27 06:36:02

标签: jsf jpa ejb glassfish-3

我正在尝试使用GlassFish,JSF / Primefaces创建一个问卷调查网站,对于数据库,我正在使用Glassfish的嵌入式德比数据库。 我按照这个视频教程(http://www.youtube.com/watch?v=aBjlR9HoR50),这是非常好的,但我没有设法让我的项目正常工作。 我的项目在Glassfish上成功部署,但是当我在浏览器上看到网页时,显然不会从xhtml页面调用支持bean。

这是支持bean代码:

package ac.hw.questionnaire;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;




import ac.hw.questionnaire.model.Question;

/**
 * Session Bean implementation class QuestionsEJB
 */
@Stateless
@LocalBean
@Named
public class QuestionsEJB {

    private Logger logging = Logger.getLogger(this.getClass().getName());

    @PersistenceContext (unitName="QuestionnaireEmbeddedDB") 
    EntityManager em;

    private List<Question> questions;

    private String name;
    /**
     * Default constructor. 
     */


    private EntityManager getEntityManager() {
        if (em==null){
            logging.info("EntityManager is null!");
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("QuestionnaireEmbeddedDB");
            em = emf.createEntityManager(); 
            if (em==null){
                logging.info("EntityManager is STILL null!");
            }
        }
            return em;


    } 
    public QuestionsEJB() {
        // TODO Auto-generated constructor stub

        logging.info("Logger: Constructor of QuestionsEJB called");
        if (getEntityManager()==null){
            return;
        }
        List resultList = em.createQuery("select q from Question q").getResultList();
        if (resultList==null){
            logging.info("ResultList is null");
        }else{
        logging.info("Constructor/Retrieved "+resultList.size()+" questions");
        }
    }

    public List<Question> getQuestions() {
        System.out.println("getQuestions method called");

        if (questions==null){
            this.questions = em.createQuery("select q from questions q").getResultList();
            System.out.println("Retrieved "+questions.size()+" questions");
        }
        return questions;
    }

    public String getMyText(){
        return "myTextFromEJB";
    }

    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }
    public String getName() {
        this.logging.info("Get name!");
        return name;
    }
    public void setName(String name) {
        this.logging.info("Set name!");
        this.name = name;
    }

}

这是我的xhtml页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<ui:composition template="WEB-INF/template.xhtml">

    <ui:define name="content">

            <h:panelGrid columns="2" cellpadding="5">
                <h:outputLabel for="myText" value="MyText is:"></h:outputLabel>
                <h:outputText id="myText" value="#{QuestionsEJB.myText}">
                </h:outputText>
            </h:panelGrid>
            <h:panelGrid columns="4" cellpadding="5">
                <h:outputLabel for="name" value="Name:" style="font-weight:bold" />

                <p:inputText id="name" value="#{QuestionsEJB.name}" />

                <p:commandButton value="Submit" update="display" />

                <h:outputText value="#{QuestionsEJB.name}" id="display" />
            </h:panelGrid>
            <br />
            <p:dataTable var="qVar" value="#{QuestionsEJB.questions}">
                <h:column>#{qVar.questionText}</h:column>
            </p:dataTable>

    </ui:define>

</ui:composition>
</html>

当我访问浏览器上的xhtml页面时,没有显示任何值,并且“提交”按钮不会执行任何操作,这会告诉我facelet未绑定到EJB。

我的web.xml(在WebContent / WEB-INF /中)具有以下内容:

<web-app version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
  <display-name>Questionnaire</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
</web-app>

我的beans.xml文件(也在WebContent / WEB-INF中)是空的(什么都没有)。 我的glassfish-web.xml文件(也在WebContent / WEB-INF中)具有以下内容:

<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <context-root>/QuestionnaireEmbeddedDB</context-root>
    <parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>

我在server.log上没有例外,唯一不正统的是EntityManager为null,直到我在QuestionEJB上显式调用getEntityManager()方法。

为什么我不能让xhtml连接到支持bean?

此外,template.xhtml具有以下内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title><ui:insert name="title">Questionnaire</ui:insert></title>
</h:head>

<body>
    <ui:debug hotkey="x"
        rendered="#{initParam['javax.faces.FACELETS_DEVELOPMENT']}" />

    <div id="header">
        <ui:insert name="header">
            <h1>Welcome to the privacy questionnaire... (header section)</h1>
            <!--  include your header file or uncomment the include below and create header.xhtml in this directory -->
            <!-- <ui:include src="header.xhtml"/> -->
        </ui:insert>
    </div>

    <h:body>
        <h:form id="mainForm" enctype="multipart/form-data">
            <div id="content">
                <ui:insert name="content">
        Content area.  See comments below this line in the source.
        <!--  include your content file or uncomment the include below and create content.xhtml in this directory -->
                    <!-- <div> -->
                    <!-- <ui:include src="content.xhtml"/> -->
                    <!-- </div> -->
                </ui:insert>
            </div>
        </h:form>
    </h:body>
    <div id="footer">
        <ui:insert name="footer">
            <br />
            <br />Powered by Glassfish &amp; Eclipse. 
        <br /> 
        <!--  include your header file or uncomment the include below and create footer.xhtml in this directory -->
            <!--<ui:include src="footer.xhtml"/>  -->
        </ui:insert>
    </div>

</body>

</html>

0 个答案:

没有答案