如何在p中使用枚举:selectOneMenu

时间:2017-04-19 15:47:39

标签: jsf primefaces enums

在我的JSF / PrimeFaces迷你网页项目中,我想使用下拉列表选择枚举项目,然后在我填写“创建”项目时将它们添加到数据库中。形式。
我使用Netbeans从数据库创建一个Entity类,然后使用PrimeFaces作为模板,从Entity类'中使用向导' JSF页面。这会自动创建创建/编辑/列表/查看xhtml页面。 目前我已经:

枚举类:

    public enum InterestsEnum {
    FOOTBALL,
    SKIING,
    CYCLING,
    MUSIC;

}

实体类:

    @Entity
public class Person implements Serializable {

    @Column(name="ID")
    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long personId;

    @Column(name="FIRSTNAME")
    @Basic(optional=false)
    @NotNull
    @Size(min=1,max=45)
    private String firstName;

    @Column(name="SURNAME")
    @Basic(optional=false)
    @NotNull
    @Size(min=1,max=45)
    private String surname;

    @ElementCollection(fetch = FetchType.EAGER, targetClass = InterestsEnum.class)
    @JoinTable(name = "Person_INTERESTS", joinColumns = @JoinColumn(name = "PERSON_ID"))
    @Column(name = "INTERESTS", nullable = false)
    @Enumerated(EnumType.STRING)
    Collection<InterestsEnum> interests;

    //Getters and Setters:

托管Bean:

@ManagedBean
@ApplicationScoped
public class Data {

    public InterestsEnum[] getInterests() {
        return InterestsEnum.values();
    }
}

PrimeFaces创建页面提取:

<p:selectOneMenu value="#{person.interests}" >
    <f:selectItems value="#{InterestsEnum}" />
</p:selectOneMenu>

PrimeFaces列表页面提取:

                        <p:column>
                        <f:facet name="header">
                            <h:outputText value="#{bundle.ListPersonTitle_interests}"/>
                        </f:facet>
                        <h:outputText value="#{item.interests}"/>
                    </p:column>

当我运行项目时,没有错误,直到我打开&#39; List&#39;,当发生以下情况时:

    Severe:   Error Rendering View[/person/List.xhtml]
javax.faces.FacesException: Cannot find component with expression "interests" referenced from "PersonCreateForm:j_idt34".
    at org.primefaces.expression.SearchExpressionFacade.resolveComponentInternal(SearchExpressionFacade.java:422)
    at org.primefaces.expression.SearchExpressionFacade.resolveComponent(SearchExpressionFacade.java:266)
    at org.primefaces.expression.SearchExpressionFacade.resolveComponent(SearchExpressionFacade.java:228)
    at org.primefaces.component.outputlabel.OutputLabelRenderer.encodeEnd(OutputLabelRenderer.java:47)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:89)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeBody(PanelGridRenderer.java:60)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:49)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:114)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:582)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at javax.faces.component.UIForm.visitTree(UIForm.java:381)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:322)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:432)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    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:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    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:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    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:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    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:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)

如果我提交了&#39;创建&#39;表单我收到以下错误:

Severe:   javax.faces.component.UpdateModelException: javax.el.PropertyNotFoundException: /person/Create.xhtml @24,71 value="#{Person.interests}": Target Unreachable, identifier 'Person' resolved to null

没有下拉列表可以选择Enum选项(参见屏幕截图)。还有&#39; List&#39;显示所有Enum成员,而不仅仅是一个。

PrimeFaces List / Create

1 个答案:

答案 0 :(得分:3)

简化我的项目并使用一个表而不是两个表并将我的Entity类更新为:

@Column(name = "INTERESTS")
@Enumerated(EnumType.STRING)
private InterestsEnum interests;

我现在有一个工作项目,允许用户在创建或更新条目时选择Enum成员。

<h:selectOneMenu value="#{personController.selected.interests}" >
                        <f:selectItems value="#{data.interests}" />
                    </h:selectOneMenu>

这可以在下面看到:

form enum choice