2015-01-02 34 views
0

我有以下p:datatable,其中包含調用View和Add對話框的兩個按鈕。 問題出在添加對話框,它似乎沒有爲任何實體對象設置屬性。p:對話框沒有將屬性設置爲實體

當我對ChildrenController.java中的save()方法進行了一次中斷時,所有值均爲'null'。我使用的是: PrimeFaces 5.1 | Mojarra 2.2.7 | Glassfish 4.1 |

ChildrenDataTable.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:p="http://primefaces.org/ui" 
      xmlns:ui="http://java.sun.com/jsf/facelets"> 
<h:form id="form1"> 
     <p:dataTable var="child" value="#{childrenController.children}" 
        scrollable="true" 
        scrollHeight="500"> 

      <p:column> 
       <f:facet name="header">First Name</f:facet>      
       <h:outputText value="#{child.firstName}" /> 
      </p:column> 
      <p:column> 
       <f:facet name="header">Last Name</f:facet> 
       <h:outputText value="#{child.lastName}" /> 
      </p:column> 
      <p:column> 
       <f:facet name="header">Parent Name</f:facet> 
       <h:outputText value="#{child.parent.firstName}" /> 
      </p:column> 
      <p:column> 
       <f:facet name="header">Parent Last Name</f:facet> 
       <h:outputText value="#{child.parent.lastName}" /> 
      </p:column> 
      <p:column style="width:32px;text-align: center"> 
       <p:commandButton update=":mainForm:form1:childDetail" 
           oncomplete="PF('childDialog').show()" 
           process="@this" 
           icon="ui-icon-search" 
           title="View"> 
        <f:setPropertyActionListener value="#{child}" 
               target="#{childrenController.selectedChild}" /> 
       </p:commandButton> 
      </p:column> 

      <p:column style="width:32px;text-align: center"> 
       <p:commandButton onclick="PF('childAddDialog').show()" 
           icon="ui-icon-person" 
           title="Add"> 
       </p:commandButton> 
      </p:column> 
     </p:dataTable> 


     <!-- Dialog for the View button--> 
     <p:dialog header="Child Info" 
        widgetVar="childDialog" 
        modal="false" 
        showEffect="fade" 
        hideEffect="fade" 
        resizable="false"> 
      <p:outputPanel id="childDetail" style="text-align:center;"> 
       <p:panelGrid columns="2" 
           rendered="#{not empty childrenController.selectedChild}" 
           columnClasses="label,value"> 
        <h:outputText value="First Name:" /> 
        <h:outputText value="#{childrenController.selectedChild.firstName}" /> 

        <h:outputText value="Last Name:" /> 
        <h:outputText value="#{childrenController.selectedChild.lastName}" /> 

        <h:outputText value="Birth Date:" /> 
        <h:outputText value="#{childrenController.selectedChild.dateOfBirth}"/> 

        <h:outputText value="Medical Info:" /> 
        <h:outputText value="#{childrenController.selectedChild.medicalInfo}" /> 
       </p:panelGrid> 
      </p:outputPanel> 
     </p:dialog> 

     <!--Dialog for the Add button--> 
     <p:dialog header="Register Child" 
        widgetVar="childAddDialog" 
        modal="false" 
        showEffect="fade" 
        hideEffect="fade" 
        resizable="false"> 
      <p:outputPanel id="childRegister" style="text-align:center;"> 
       <p:panelGrid columns="2" > 
        <f:facet name="header"> 
         Child info 
        </f:facet> 
        <h:outputText value="First Name:"/> 
        <h:inputText value="#{childrenController.child.firstName}" 
           required="true"/> 

        <h:outputText value="Last Name:"/> 
        <h:inputText value="#{childrenController.child.lastName}" 
           required="true"/> 

        <h:outputText value="Birth Date:"/> 
        <h:inputText value="#{childrenController.child.dateOfBirth}" 
           required="true"/> 

        <h:outputText value="Medical Info:"/> 
        <p:inputTextarea value="#{childrenController.child.medicalInfo}" 
            rows="5" cols="30" counter="display" maxlength="128" 
            counterTemplate="{0} characters remaining." autoResize="false"/> 
        <h:outputText id="display" /> 
       </p:panelGrid> 

       <p:panelGrid columns="2" > 
        <f:facet name="header"> 
         Parent info 
        </f:facet> 
        <h:outputText value="First Name:"/> 
        <h:inputText value="#{childrenController.parent.firstName}" 
           required="true"/> 

        <h:outputText value="Last Name:"/> 
        <h:inputText value="#{childrenController.parent.lastName}" 
           required="true"/> 
        <h:outputText value="Username:"/> 
        <h:inputText value="#{childrenController.user.username}" 
           required="true"/> 

        <h:outputText value="Password1:"/> 
        <p:password id="pwd1" value="#{childrenController.password1}" 
           match="pwd2" 
           required="true"/> 
        <h:outputText value="Password2:"/> 
        <p:password id="pwd2" value="#{childrenController.user.password}" 
           required="true"/> 

        <p:commandButton actionListener="#{childrenController.save()}" 
             value="Save" 
             process="@this" 
             oncomplete="childAddDialog.hide()"> 
        </p:commandButton> 
       </p:panelGrid> 
      </p:outputPanel> 
     </p:dialog> 
    </h:form> 
</ui:composition> 

ChildrenController.java

@Named 
@SessionScoped 
public class ChildrenController implements Serializable{ 
    // This final string will be used to set the loginn role to parent 
    private static final String parentRole = "parent"; 
    private String password1; 


@EJB 
private ChildEJB childEJB; 
private Child currentChild; 

private Child child = new Child(); 

@EJB 
private ParentEJB parentEJB; 
private Parent parent = new Parent(); 

@EJB 
private RoleEJB roleEJB; 
private Role role = new Role(parentRole); 

@EJB 
private UserEJB userEJB; 
private User user = new User(); 

public Child getCurrentChild() { 
    return currentChild; 
} 

public void setCurrentChild(Child currentChild) { 
    this.currentChild = currentChild; 
} 

public Child getChild() { 
    return child; 
} 

public void setChild(Child child) { 
    this.child = child; 
} 

public Parent getParent() { 
    return parent; 
} 

public void setParent(Parent parent) { 
    this.parent = parent; 
} 

public User getUser() { 
    return user; 
} 

public void setUser(User user) { 
    this.user = user; 
} 

public Role getRole() { 
    return role; 
} 

public void setRole(Role role) { 
    this.role = role; 
} 

public Child getSelectedChild() { 
    return selectedChild; 
} 

public void setSelectedChild(Child selectedChild) { 
    this.selectedChild = selectedChild; 
} 
private Child selectedChild; 

public List<Child> getChildren() { 
    return childEJB.findAll(); 
} 

    public String getPassword1() { 
    return password1; 
} 

public void setPassword1(String password1) { 
    this.password1 = password1; 
} 

public void save(){ 
    // Map the parent to the child and vice verca 
    parent.addChild(child); 
    child.setParent(parent); 

    // Persist the Parent 
    parent = parentEJB.addNew(parent); 

    // Persist the Child 
    child = childEJB.addNew(child); 

    // This will set the username for the parent role 
    role.setUsername(user.getUsername()); 

    // This will set the role for the new username 
    user.setRole(role); 

    // This will map that the login belongs to a Parent 
    user.setParent(parent); 

    // Persist the new User 
    user = userEJB.addNew(user); 
} 

}

堆棧跟蹤

Warning: A system exception occurred during an invocation on EJB ParentEJB, method: public entity.Parent EJB.ParentEJB.addNew(entity.Parent) 
Warning: javax.ejb.EJBException 
    at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748) 
    at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:698) 
    at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503) 
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566) 
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074) 
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) 
    at com.sun.proxy.$Proxy250.addNew(Unknown Source) 
    at EJB.__EJB31_Generated__ParentEJB__Intf____Bean__.addNew(Unknown Source) 
    at mb.ChildrenController.save(ChildrenController.java:110) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at javax.el.ELUtil.invokeMethod(ELUtil.java:332) 
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:537) 
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:283) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) 
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [entity.Parent] during persist time for groups [javax.validation.groups.Default, ] 
List of constraint violations:[ 
    ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=lastName, rootBeanClass=class entity.Parent, messageTemplate='{javax.validation.constraints.NotNull.message}'} 
    ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=firstName, rootBeanClass=class entity.Parent, messageTemplate='{javax.validation.constraints.NotNull.message}'} 
] 
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:160) 
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:95) 
    at org.hibernate.action.internal.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:202) 
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:91) 
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:480) 
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:191) 
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:175) 
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:210) 
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) 
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84) 
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206) 
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149) 
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75) 
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:807) 
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:780) 
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:785) 
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181) 
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287) 
    at EJB.ParentEJB.addNew(ParentEJB.java:31) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608) 
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46) 
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52) 
    at sun.reflect.GeneratedMethodAccessor166.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140) 
    at sun.reflect.GeneratedMethodAccessor168.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) 
    ... 54 more 
+0

異常stacktrace指示另一個圖層。 stacktrace中的這一行:'javax.validation.ConstraintViolationException:類[entity.Parent]的驗證失敗'可能意味着您應該從您的客戶端應用程序(CDI bean)將這個分離的實體'Parent'傳遞給一個EJB在持久化/合併導致所述異常的實體期間,由Hibernate Validator對null值進行驗證的'null'字段/屬性(使用'@NotNull'約束)。確保「Parent」實體中沒有任何字段違反了指定的約束條件。 – Tiny

+0

當我在save()方法上放置一個斷點並檢查所有值時,它們都是NULL。 Parent,Child和User的每個值都是null,就像我在對話框的inputText文件中輸入的內容一樣。 –

+0

爲什麼您將這些值直接設置爲實體的屬性?相反,您最好在託管bean中聲明所需類型的各個屬性,將這些屬性值作爲方法參數傳遞給目標EJB,並使用這些屬性/字段的值在EJB中執行業務操作/操作。 – Tiny

回答

0

你沒有process你在你的問題的代碼組件。您在那裏解決問題的方法是在您要處理的UIComponent列表中包含您的form。這意味着JSF不會查看您的form中的所有輸入組件,這就是爲什麼它不會檢索您在這些字段中輸入的任何值,以將它們設置在您的支持bean中。

爲了更好地理解過程的含義,你可以看看at this question

0

解決了這個問題,把p:commandButton放在對話框的p:panelGrid裏,這對我來說絕對沒有意義!反正這裏是對話的樣子:

<h:form id="formName"> 
     <p:dialog> 
      <p:panelGrid> 

      <p:commandButton process="@this :formName" ajax="false" actionListener="#{bean.method()}"/> 
      </p:panelGrid> 
     </p:dialog> 
</h:form> 

編輯:編輯的代碼更可讀

+0

說得很真誠:'process =「@ this:formName」'當'ajax'設置爲'false'時沒有任何意義。你真的明白你在這個代碼中究竟做了什麼? – Tiny

相關問題