Setting two p:selectOneMenu s

你。 提交于 2019-12-24 09:16:21

问题


I need to use two primefaces selectonmenus to my web page.

  1. My second selectonmenu works when it has static data all data can view in button click after press the button

  2. But it not work with dynamic data data get load to this after selecting a value from 1st selectonmenu using an ajax data load to this selectonmenu but it always show null value after select a value from this

this the code for point 1

<p:tab id="create_Subnet_T" title="Create subnet">
<h:form id="create_Subnet">
    <h:panelGrid columns="2" cellpadding="10">

        <h:outputText value="Area:"/>
        <p:selectOneMenu id="AreaDDL" value="#{a_Subnet.selectedArea}" required="true">  
            <f:selectItem itemLabel="Select area" itemValue="" />  
            <f:selectItems value="#{a_Subnet.areaList}" />


        </p:selectOneMenu>

        <h:outputText id="create_Subnet_OT" value="IP Address :" />
        <p:selectOneMenu id="resourceDDL" value="#{a_Subnet.selectedResource}" required="true">  
            <f:selectItem itemLabel="Select resource" itemValue="A" />
            <f:selectItem itemLabel="Select resource" itemValue="B" />
            <f:selectItem itemLabel="Select resource" itemValue="C" />

        </p:selectOneMenu>

        <h:outputText id="netmask_OT" value="netmask :"  />
        <p:inputText id="netmask_IT"  required="true" value="#{a_Subnet.netmask}"/>

        <h:outputText id="description_OT" value="Description :" />
        <p:inputTextarea id="description_ITA" required="true" value="#{a_Subnet.description}"/>

        <p:commandButton id="create_Subnet_Btn" value="Create"  action="#{a_Subnet.test}" />

    </h:panelGrid>
</h:form>

This the code that not working (for point 2):

<p:tab id="create_Subnet_T" title="Create subnet">
    <h:form id="create_Subnet">
        <h:panelGrid columns="2" cellpadding="10">

            <h:outputText value="Area:"/>
            <p:selectOneMenu id="AreaDDL" value="#{a_Subnet.selectedArea}" required="true">  
                <f:selectItem itemLabel="Select area" itemValue="" />  
                <f:selectItems value="#{a_Subnet.areaList}" />

                <p:ajax event="change" update=":Subnet_TV:create_Subnet:resourceDDL"
                        listener="#{a_Subnet.setResourceToDropDownList(a_Subnet.selectedArea)}"/> 
            </p:selectOneMenu>

            <h:outputText id="create_Subnet_OT" value="IP Address :" />
            <p:selectOneMenu id="resourceDDL" value="#{a_Subnet.selectedResource}" required="true">  
                <f:selectItem itemLabel="Select resource" itemValue="A" />
                <f:selectItems value="#{a_Subnet.resourceList}" />
            </p:selectOneMenu>

            <h:outputText id="netmask_OT" value="netmask :"  />
            <p:inputText id="netmask_IT"  required="true" value="#{a_Subnet.netmask}"/>

            <h:outputText id="description_OT" value="Description :" />
            <p:inputTextarea id="description_ITA" required="true" value="#{a_Subnet.description}"/>

            <p:commandButton id="create_Subnet_Btn" value="Create"  action="#{a_Subnet.test}" />

        </h:panelGrid>
    </h:form>
</p:tab>

This is the method used in ajax for check the output:

public void test(){
    System.out.println("hjhjjh");
    System.err.println( "Area = "+selectedArea);
    System.err.println("Resource = "+selectedResource);
    System.err.println("SNetmask = "+netmask);
    System.err.println("Description = "+description);

}

回答1:


When you deal with ajax operations in the same page, it is recommended that your managed bean have at least @ViewScoped annotation since the bean in this scope lives as long as you're interacting with the same JSF view in the browser window/tab (from Communication in JSF 2: Managed Bean Scopes).

If you're using CDI annotations (like @Named), then you won't have the @ViewScoped annotation. In order to make it available for CDI, you should add MyFaces CODI.

Just as an additional recommendation, don't write business logic server inside the getters/setters methods since getters get called multiple times.



来源:https://stackoverflow.com/questions/15239589/setting-two-pselectonemenu-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!