How to save 2 dependent selectOneMenu values

青春壹個敷衍的年華 提交于 2019-12-22 18:05:25

问题


I'm running in a little issue here regarding jsf related dropdown selection. I have 2 dropdown selection: the first one is independent, the second one shows result depending from This is the code:

<h:panelGroup id="addressPanel">        
        <h:outputLabel styleClass="label" value="Provincia: " />
        <h:selectOneMenu onchange="updateCombos()"
            value="#{indirizzoCtrl.codiceProvincia}"  >
            <f:selectItem itemValue="" itemLabel=""></f:selectItem>
            <f:selectItems value="#{indirizzoCtrl.allProvincia}" var="c"
                itemLabel="#{c.nome}" itemValue="#{c.siglaProvincia}" />
        </h:selectOneMenu>
        <h:outputLabel styleClass="label" value="Comune: " />
        <h:selectOneMenu 
            value="#{indirizzoCtrl.codiceComune}"  >
            <f:selectItem itemValue="" itemLabel=""></f:selectItem>
            <f:selectItems value="#{indirizzoCtrl.allComuni}" var="c"
                itemLabel="#{c.descrizione}" itemValue="#{c.codiceComune}" />

        </h:selectOneMenu>
    </h:panelGrid>
</h:panelGroup>
<p:remoteCommand name="updateCombos" update="addressPanel masterForm:msg"  />              
<p:commandButton styleClass="commandButton" value="Save"
    actionListener="#{indirizzoCtrl.save}">
</p:commandButton>

Well, when the user save the form after selecting both the value, the managed bean indirizzoCtrl (request scoped) cannot map the value of the second dropdown back to the list, because there's no list. In fact the #{indirizzoCtrl.allComuni} call a getter that retrieve the data from the DB only if indirizzoCtrl.codiceProvincia!=null... and that's false before the update model phase. So the first time the getter for the list is called cannot retrieve any values, and that's bring the update model phase to fail. How can I handle this scenario... I think it's a pretty common one, so I'm missing something here...


回答1:


Put the bean in the view scope instead. It will live as long as you're interacting with the same view by ajax. A request scoped bean get indeed recreated on every single request, also ajax requests, so bean properties would reinitialize to their defaults.

@ManagedBean
@ViewScoped
public class IndidizzoCtrl implements Serializable {

    // ...

}

See also:

  • How to load and display dependent h:selectOneMenu on change of a h:selectOneMenu
  • How to choose the right bean scope?

Unrelated to the concrete problem, I also strongly recommend to change your getter methods to not do any business job, but just return data. Do the business job in (action)listener methods instead. E.g.

<h:selectOneMenu value="#{bean.menu1item}">
    <f:selectItems value="#{bean.menu1items}" />
    <f:ajax listener="#{bean.updateMenu2}" render="menu2" />
</h:selectOneMenu>
<h:selectOneMenu id="menu2" value="#{bean.menu2item}">
    <f:selectItems value="#{bean.menu2items}" />
</h:selectOneMenu>

with

public void updateMenu2() {
    menu2items = loadItBasedOn(menu1item);
}

See also:

  • Why JSF calls getters multiple times


来源:https://stackoverflow.com/questions/8537464/how-to-save-2-dependent-selectonemenu-values

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