1

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);

}
sithila
  • 27
  • 7
  • Make sure your `a_Subnet` managed bean is at least `@ViewScoped`. – Luiggi Mendoza Mar 06 '13 at 05:29
  • managed bean class is in "@requestScope" change it to "@ViewScoped" nothin happens – sithila Mar 06 '13 at 05:41
  • Change it to `@ViewScoped` if you're using `javax.faces` package annotations. If you're using CDI, you should set them using `@SessionScoped` or getting the `@ViewScoped` from [MyFaces CODI](http://myfaces.apache.org/extensions/cdi/) – Luiggi Mendoza Mar 06 '13 at 05:42

1 Answers1

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.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332