我正在使用带有Glassfish 3.1.2的NetBeans 7.1来开发一个简单的Java EE 6 Web应用程序(一种地址簿)。我在网上广泛搜索了我的问题,但没有有用的结果。
我的应用程序使用PrimeFaces 3.2和JSF 2.1.7。
文件ContactsBean.java
package com.organizer.ejb;
import com.organizer.entity.Contact;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
@ManagedBean
@RequestScoped
public class ContactsBean {
private List<Contact> contacts;
private Contact contact=new Contact();
@PersistenceUnit(unitName="PersonalOrganizerJPA")
private EntityManagerFactory emf;
@Resource
private UserTransaction utx;
public ContactsBean()
{
}
public List<Contact> getContacts() {
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT c FROM Contact c");
contacts = (List<Contact>)query.getResultList();
return contacts;
}
public void salva(ActionEvent event){
EntityManager em = emf.createEntityManager();
try {
utx.begin();
em.joinTransaction();
em.persist(contact);
utx.commit();
showGrowlInsMessage();
} catch (Exception e) {
try {
utx.rollback();
} catch (IllegalStateException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
} catch (SecurityException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
} catch (SystemException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
}
}finally{
em.close();
}
contact=null;
}
public void aggiorna(ActionEvent event){
EntityManager em = emf.createEntityManager();
try {
utx.begin();
em.joinTransaction();
em.merge(contact);
utx.commit();
showGrowlUpdMessage();
} catch (Exception e) {
try {
utx.rollback();
} catch (IllegalStateException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
} catch (SecurityException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
} catch (SystemException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
}
}finally{
em.close();
}
contact=null;
}
public void elimina(ActionEvent event){
EntityManager em = emf.createEntityManager();
try {
utx.begin();
em.joinTransaction();
Contact c = em.find(Contact.class, contact.getId());
em.remove(c);
utx.commit();
this.showGrowlDelMessage();
} catch (Exception e) {
try {
utx.rollback();
} catch (IllegalStateException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
} catch (SecurityException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
} catch (SystemException e1) {
showGrowlErrorMessage();
e1.printStackTrace();
}
}finally{
em.close();
}
contact=null;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
private void showGrowlUpdMessage(){
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "Contatto aggiornato con successo"));
}
private void showGrowlInsMessage(){
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "Contatto inserito con successo"));
}
private void showGrowlDelMessage(){
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "Contatto eliminato con successo"));
}
private void showGrowlErrorMessage(){
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Errore", "Operazione fallita"));
}
}
文件web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 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-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/ContactsPage.xhtml</welcome-file>
</welcome-file-list>
</web-app>
文件ContactsPage.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Contacts</title>
<p:resources />
<style>
form{font-size:12px;}
</style>
</h:head>
<h:body>
<h:form>
<p:spacer height="10"></p:spacer>
<p:graphicImage value="Contacts" fontName="Tahoma" fontStyle="bold" fontSize="24"/>
<p:spacer height="10"/>
<p:panel header="Contacts">
<p:dataTable var="contact" value="#{contactsBean.contacts}" id="tbl"
selection="#{contactsBean.contact}" selectionMode="single"
update="updForm:updDisplay" onselectComplete="updDialog.show()"
paginator="true" rows="5">
<p:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{contact.name}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Cognome" />
</f:facet>
<h:outputText value="#{contact.surname}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Telefono" />
</f:facet>
<h:outputText value="#{contact.telephone}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Professione" />
</f:facet>
<h:outputText value="#{contact.job}" />
</p:column>
<f:facet name="footer">
<h:panelGroup>
<h:commandLink>
<p:graphicImage value="/images/excel.png" style="border:none" />
<p:dataExporter type="xls" target="tbl" fileName="contacts"
pageOnly="true" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/images/pdf.png" style="border:none" />
<p:dataExporter type="pdf" target="tbl" fileName="contacts"
pageOnly="true" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/images/csv.png" style="border:none" />
<p:dataExporter type="csv" target="tbl" fileName="contacts"
pageOnly="true" />
</h:commandLink>
<h:commandLink>
<p:graphicImage value="/images/xml.png" style="border:none" />
<p:dataExporter type="xml" target="tbl" fileName="contacts"
pageOnly="true" />
</h:commandLink>
</h:panelGroup>
</f:facet>
</p:dataTable>
</p:panel>
</h:form>
<p:spacer height="10" />
<h:form id="updForm">
<h:panelGrid columns="2">
<p:commandButton oncomplete="newDialog.show()" value="Nuovo Contatto"
update="newDisplay" ajax="true" />
<p:themeSwitcher initialText="Cambia tema"></p:themeSwitcher>
</h:panelGrid>
<p:dialog widgetVar="updDialog" header="Contatto" width="430"
height="220" modal="true">
<p:outputPanel id="updDisplay">
<h:inputHidden value="#{contactsBean.contact.id}" />
<h:panelGrid columns="2">
<h:outputLabel value="Nome" />
<p:inputText value="#{contactsBean.contact.name}"></p:inputText>
<h:outputLabel value="Cognome" />
<p:inputText value="#{contactsBean.contact.surname}"></p:inputText>
<h:outputLabel value="Telefono" />
<p:inputText value="#{contactsBean.contact.telephone}"></p:inputText>
<h:outputLabel value="Professione" />
<p:inputText value="#{contactsBean.contact.job}"></p:inputText>
</h:panelGrid>
<p:commandButton value="Aggiorna" ajax="true"
actionListener="#{contactsBean.aggiorna}" update="tbl,growl"
onclick="updDialog.close()"></p:commandButton>
<p:commandButton value="Elimina" ajax="true"
actionListener="#{contactsBean.elimina}" update="tbl,growl"
onclick="updDialog.close()"></p:commandButton>
<p:growl id="growl" showDetail="true" />
</p:outputPanel>
</p:dialog>
</h:form>
<h:form>
<p:dialog widgetVar="newDialog" header="Inserisci Contatto"
width="430" height="220" modal="true">
<p:outputPanel id="newDisplay">
<h:panelGrid columns="2">
<h:outputLabel value="Nome" />
<p:inputText value="#{contactsBean.contact.name}"></p:inputText>
<h:outputLabel value="Cognome" />
<p:inputText value="#{contactsBean.contact.surname}"></p:inputText>
<h:outputLabel value="Telefono" />
<p:inputText value="#{contactsBean.contact.telephone}"></p:inputText>
<h:outputLabel value="Professione" />
<p:inputText value="#{contactsBean.contact.job}"></p:inputText>
</h:panelGrid>
<p:commandButton value="Salva" ajax="true"
actionListener="#{contactsBean.salva}" update="tbl"
onclick="newDialog.close()"></p:commandButton>
</p:outputPanel>
</p:dialog>
</h:form>
</h:body>
</html>
这是GlassFish给我的警告。
Warning: WEB9052: Unable to load class com.organizer.ejb.ContactsBean, reason: java.lang.ClassNotFoundException: com.organizer.ejb.ContactsBean
异常堆栈跟踪。
Warning: Cannot create update center Image for C:\glassfish3; Update Center functionality will not be available in Admin Console
Information: p:resources component is deprecated and has no use in PrimeFaces 2.0 as JSF 2.0 resource apis are used instead to place resources on page.
Grave: Error Rendering View[/ContactsPage.xhtml]
javax.el.PropertyNotFoundException: /ContactsPage.xhtml @28,31 value="#{contactsBean.contacts}": Target Unreachable, identifier 'contactsBean' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at org.primefaces.component.datatable.DataTable.isLazy(DataTable.java:968)
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:191)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:108)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:61)
at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:45)
at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:185)
at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:108)
at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:55)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
编辑:这是整个应用程序的结构。 Link