开发者

JSF 2.0 Input field rendered by Ajax cannot be used to input values?

开发者 https://www.devze.com 2023-01-12 22:57 出处:网络
<h:form> Do you have a driving license? <h:selectOneMenu value=\"#{requestScope.license}\">
<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.


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

精彩评论

暂无评论...
验证码 换一张
取 消