English 中文(简体)
How to set dropdown with values depending on another dropdown if both are set to required
原标题:

Can anyone tell me how to automatically set <h:selectOneMenu> (or any other component) with values depending on another <h:selectOneMenu> if there empty elements with required set to true on the form? If to set <a4j:support event="onchange" reRender="anotherElement" immediate="true" /> then nothing is changed because changed value isn t set. But without immediate="true" I always have message that this or that element cannot be empty. Here s code example that doesn t work.

<h:outputLabel value="* #{msg.someField}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:selectOneMenu id="someSelect"
            value="#{MyBean.someObj.someId}"
            required="true" label="#{msg.someField}"
            >
        <a4j:support event="onchange" reRender="anotherSelect" limitToList="true" immediate="true"/>
        <f:selectItem itemValue=""/>
        <f:selectItems value="#{MyBean.someList}"/>
    </h:selectOneMenu>
    <rich:message for="someSelect" styleClass="redOne"/>
</h:panelGrid>

<h:outputLabel value="* #{msg.anotherField}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:selectOneMenu id="anotherSelect"
            value="#{MyBean.someObj.anotherId}"
            required="true" label="#{msg.anotherField}"
            >
        <f:selectItem itemValue=""/>
        <f:selectItems value="#{MyBean.anotherList}"/>
    </h:selectOneMenu>
    <rich:message for="anotherSelect" styleClass="redOne"/>
</h:panelGrid>

<h:outputLabel value="* #{msg.name}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:inputText id="myName" value="#{MyBean.someObj.myName}" 
            required="true" label="#{msg.name}"/>
    <rich:message for="myName" styleClass="redOne"/>
</h:panelGrid>

So, here (I repeat), if I try to change someSelect then anotherSelect should update its values but it doesn t because either when it tries to get value of someSelect it gets null (if immediate set to true) or form validation fails on empty elements. How can I skip validation but get this changed value from someSelect ?

最佳回答

Have you tried adding ajaxSingle="true" to the someSelect a4j:support element? Remove the immediate="true"

问题回答

The following solution works in JSF 2.0

<h:outputLabel
    value="* Country: "/>
<h:selectOneMenu
    id="someSelect"
    value="#{testController.countryName}"
    required="true">
    <f:selectItem
        itemLabel="Select Country"
        itemValue=""/>
    <f:selectItems
        value="#{testController.countryNamesSelectItems}"/>
    <!-- This will only update "someSelect" and repaint "anotherSelect" -->
    <f:ajax
        execute="@this"
        render="anotherSelect"/>
</h:selectOneMenu>

<h:outputLabel
    value="* State: "/>
<h:selectOneMenu
    id="anotherSelect"
    value="#{testController.stateName}"
    required="true">
    <f:selectItem
        itemLabel="Select State"
        itemValue=""/>
    <f:selectItems
        value="#{testController.stateNamesSelectItems}"/>
</h:selectOneMenu>

The f:ajax call will submit an ajax request to the server onchange of "someSelect". The model update for only the "someSelect" component will happen (because of execute="@this"). In the render response phase, only "anotherSelect" will be re-rendered (thus invoking testController.stateNamesSelectItems with the updated country name).

As a result of all this, the states of the selected country will get updated as an when country is changed...the ajax way.

Hope this helps.





相关问题
JSF a4j:support with h:selectManyCheckbox

I m having trouble with a JSF selectManyCheckbox and A4J support. The purpose is to run some action when a checkbox is selected. This works perfectly in Firefox. Yet, when testing in any IE (ie6 / ie7 ...

Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

如何拦截要求终止?

在共同基金中,如果用户要求终止,就需要采取一些行动。 我需要某种拦截器,但我不知道如何这样做。 我需要帮助。 增 编

ICEFaces inputFile getting the file content without upload

Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

热门标签