p:dialog 没有为 Entity 设置属性

p:dialog does not set properties to Entity

我有以下 p:datatable,其中包含两个调用“查看”和“添加”对话框的按钮。 问题在于“添加”对话框,它似乎没有为任何实体对象设置属性。

当我在 ChildrenController.java 内的 save() 方法上打断时,所有值都是 'null'。 我在用: PrimeFaces 5.1 | 莫哈拉 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.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.access0(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

通过将 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>

编辑:编辑代码以提高可读性

您没有 process 您问题代码中的组件。您在那里为解决您自己的答案中的问题所做的工作是将您的 form 包含在要处理的 UIComponent 列表中。这意味着 JSF 不会查看 form 中的所有输入组件,这就是为什么它不会检索您在这些字段中输入的任何值以将它们设置到支持 bean 中的原因。

为了更好地理解进程的含义,您可以查看 at this question