如何保存2个依赖的selectOneMenu值 [英] How to save 2 dependent selectOneMenu values

查看:36
本文介绍了如何保存2个依赖的selectOneMenu值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了一个与jsf相关的下拉菜单选择的小问题. 我有2个下拉菜单选择:第一个是独立的,第二个显示取决于的结果,这取决于代码:

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>

好吧,当用户在选择了两个值之后保存表单时,托管bean indirizzoCtrl(请求范围)无法将第二个下拉菜单的值映射回列表,因为没有列表. 实际上,#{indirizzoCtrl.allComuni}调用仅当indirizzoCtrl.codiceProvincia!=null ...时才从DB检索数据的getter,并且在更新模型阶段之前为false. 因此,第一次调用列表的getter不能检索任何值,这导致更新模型阶段失败. 我该如何处理这种情况...我认为这是一种很常见的情况,所以我在这里遗漏了一些东西...

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...

推荐答案

将bean放入视图范围.只要您通过ajax与同一视图进行交互,它就会一直存在.实际上,在每个单个请求(也包括ajax请求)上都会重新创建一个请求范围的Bean,因此Bean属性将重新初始化为其默认值.

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 {

    // ...

}

另请参见:

  • 如何加载和显示h:selectOneMenu取决于ah:selectOneMenu的更改
  • 如何选择正确的bean作用域?
  • 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>
      

      使用

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

      另请参见:

      • 为什么JSF多次调用getters
      • See also:

        • Why JSF calls getters multiple times
        • 这篇关于如何保存2个依赖的selectOneMenu值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆