tags:

views:

53

answers:

2
<h:form>
    Do you have a driving license?
    <h:selectOneMenu value="#{requestScope.license}">
        <f:selectItem itemLabel="Select..." itemValue=""/>
        <f:selectItem itemLabel="Yes" itemValue="Y"/>
        <f:selectItem itemLabel="No" itemValue="N"/>
        <f:ajax render="@form"/>
    </h:selectOneMenu>
    <br/>
    Enter driving license number:
    <h:inputText value="#{requestScope.number}"
      rendered="#{'Y' eq requestScope.license ? true : false}"/>
    <br/>
    <h:commandButton value="Submit"/>
    <br/>
    Your driving license number is: #{requestScope.number}
</h:form>

Problem in implementing a VERY COMMON scenario with JSF 2.0

The above JSF 2.0 markup depicts a very common scenario.

If the user selects “Yes” for the question “Do you have a driving license?”, then the “Enter your driving license number:” field is rendered by Ajax.

If the user enters the driving license number and presses the button, the input data should be displayed at the bottom (according to my understanding), but it is not happening.

Please anyone explain this.

A: 

Go For RichFaces,
Render the text input on click of checkbox.

org.life.java
+1  A: 

You need to bind the properties to a fullworthy managed bean and display the number in a h:outputText so that it can be rerendered.

Here's the improvement.

<h:form>
    <h:outputLabel for="license" value="Do you have a driving license?" />
    <h:selectOneMenu id="license" value="#{bean.license}" required="true">
        <f:selectItem itemLabel="Select..." itemValue="" />
        <f:selectItem itemLabel="Yes" itemValue="Y" />
        <f:selectItem itemLabel="No" itemValue="N" />
        <f:ajax render="@form"/>
    </h:selectOneMenu>
    <br/>
    <h:panelGroup id="input" rendered="#{bean.license eq 'Y'}">
        <h:outputLabel for="number" value="Enter driving license number:" />
        <h:inputText id="number" value="#{bean.number}" required="true" />
        <br/>
    </h:panelGroup>
    <h:commandButton value="Submit" action="#{bean.submit}">
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
    <br/>
    <h:outputText value="Your driving license number is: #{bean.number}" 
        rendered="#{bean.license eq 'Y' and not empty bean.number}" />
    <br/>
    <h:messages />
</h:form>

With a bean like this:

@ManagedBean
@ViewScoped
public class Bean {

    private String license;
    private Integer number;

    public void submit() {
        System.out.println("Has license? " + license);
        System.out.println("Submitted number: " + number);
    }

    // Add/generate getters and setters.
}
BalusC