Updating text component via selectOneMenu from inside a JSF2/Facelets subview

北战南征 提交于 2019-12-24 07:54:21

问题


I have a Facelets subview called basedata-cpanel.xhtml, which gets its parameters passed via <ui:param>s:

<ui:define name="content-top">
  <h:form>
    <ui:include src="/subviews/basedata-cpanel.xhtml">
      <ui:param name="customerId"   value="#{pqHome.pq.customer.id}" />
      <ui:param name="customerName" value="#{pqHome.pq.customer.name}" />
      <ui:param name="customers"    value="#{organizationManager.allCustomers}" />        
    <ui:include />
  </h:form>
</ui:define>

In the subview, I simply want a panel below to show the currently selected customer name (the panel is for NEW and UPDATE panels/pages). I need an AJAX request for this.

Code in /subviews/basedata-cpanel.xhtml:

      <h:selectOneMenu value="#{customerId}" id="customer-select">
        <f:selectItems value="#{customers}"
                       var="customer"
                       itemValue="#{customer.id}"
                       itemLabel="#{customer.name}" />
        <f:ajax render="customer-name" />
      </h:selectOneMenu>
      <h:outputText value="#{customerName}" id="customer-name" />

I put the <f:ajax render="customer-name" /> into the select, but the outputText isn't updated. I didn't really expect it to, but...

What do I need to do to get it to work?

As the Facelets subview still has the same parameters as they were passed, how do I get "new ones" in? What's the best practice for this?

PS: pqHome is a @ViewScoped bean


回答1:


Your dropdown only changes the ID of the customer, not the customer name. Even more, it does not change the whole customer entity at all. It only changes the ID of an existing customer. This is not good.

You need to change the code to let it set the whole customer instead of only its ID. E.g.

<ui:include src="/subviews/basedata-cpanel.xhtml">
    <ui:param name="customer" value="#{pqHome.pq.customer}" />
    <ui:param name="customers" value="#{organizationManager.allCustomers}" />        
</ui:include>

with

<h:selectOneMenu value="#{customer}" id="customer-select" converter="customerConverter">
    <f:selectItems value="#{customers}" var="c" itemValue="#{c}" itemLabel="#{c.name}" />
    <f:ajax render="customer-name" />
</h:selectOneMenu>
<h:outputText value="#{customer.name}" id="customer-name" />

Don't forget to create a Converter which converts between customer ID and the Customer object and use it in the <h:selectOneMenu>.



来源:https://stackoverflow.com/questions/10295790/updating-text-component-via-selectonemenu-from-inside-a-jsf2-facelets-subview

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