不会从< h:selectOneRadio>中调用valueChangeListener.放置在< h:panelGrid>侧面 [英] valueChangeListener is not getting called from <h:selectOneRadio> which is placed in side a <h:panelGrid>

查看:93
本文介绍了不会从< h:selectOneRadio>中调用valueChangeListener.放置在< h:panelGrid>侧面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正遇到h:selectOneRadio的valueChangeListener =#{user.loadYesNo}"的问题 (我在Tomcat-7上使用Mojarra 2-0-8). 如果我同时删除了两个包含'h:selectOneRadio'的panelGrid,则将触发值更改修饰符.

I am facing an issue with h:selectOneRadio's valueChangeListener="#{user.loadYesNo}" (I use Mojarra 2-0-8 on Tomcat-7) . If I remove both the panelGrid enclosing the 'h:selectOneRadio', then the value change litener is getting fired.

查看:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<h:head><title> Starting JSF</title></h:head>
<h:body>
<h:form>
<h:panelGrid column="2">
    <h:outputLabel>User Name</h:outputLabel>
    <h:inputText id="loginName" value="#{user.userName}"></h:inputText>
    <h:outputLabel>Password</h:outputLabel>
    <h:inputSecret id="loginPassword" value="#{user.password}"></h:inputSecret>
</h:panelGrid>
<h:commandButton value="Submit" action ="#{user.validateLogin}">
    <f:ajax execute="@form" render="yesNoRadioGrid message"></f:ajax>
</h:commandButton>
<h:panelGrid>
    <h:outputText id ="message" value="#{user.message}"></h:outputText>
</h:panelGrid>
<h:panelGrid  id="yesNoRadioGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadioGridFlag}">
    <h:outputText id ="otherLbl" value="Select Yes or No"></h:outputText>
    <h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}" valueChangeListener="#{user.loadYesNo}">
        <f:selectItem itemValue="1"  itemLabel="YES"></f:selectItem>
        <f:selectItem itemValue="0" itemLabel="NO"></f:selectItem>
        <f:ajax event="change" execute="@form" render="userDetailsGrid "></f:ajax>
    </h:selectOneRadio>
</h:panelGrid>
</h:panelGrid>      
<h:message for ="yesNoRadio"> </h:message>
<h:panelGrid  id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.userDetailsGridFlag}">
    <h:outputLabel>Name :</h:outputLabel>
    <h:inputText id="customerName" value="#{user.customerName}"></h:inputText>
    <h:outputLabel>Salary: </h:outputLabel>
    <h:inputText id="customerSalary" value="#{user.customerSalary}"></h:inputText>
</h:panelGrid>
</h:panelGrid>      
</h:form>
</h:body>
</html>   

模型+控制器混合在一起:

Model+Controller mingled:

    package com.jsf.test;
import javax.faces.bean.*;
import javax.faces.event.ValueChangeEvent;
@ManagedBean
public class User {

private String userName;
private String password;

private String message; 
private String  customerName;
private String  customerSalary;

private Integer yesNoRadio;

private Boolean yesNoRadioGridFlag;
private Boolean userDetailsGridFlag;

public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}

public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerSalary() {
return customerSalary;
}
public void setCustomerSalary(String customerSalary) {
this.customerSalary = customerSalary;
}

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getYesNoRadio() {
return yesNoRadio;
}
public void setYesNoRadio(Integer yesNoRadio) {
this.yesNoRadio = yesNoRadio;
}
public Boolean getUserDetailsGridFlag() {
return userDetailsGridFlag;
}
public void setUserDetailsGridFlag(Boolean userDetailsGridFlag) {
this.userDetailsGridFlag = userDetailsGridFlag;
}
public Boolean getYesNoRadioGridFlag() {
return yesNoRadioGridFlag;
}
public void setYesNoRadioGridFlag(Boolean yesNoRadioGridFlag) {
this.yesNoRadioGridFlag = yesNoRadioGridFlag;
}

public String validateLogin() {
if (userName.equals("xyz") && password.equals("xyz")) {
    message = "Login Success";
    yesNoRadioGridFlag = true;
} else {
    yesNoRadioGridFlag = false;
    message = "Login Failure";  
}
    return message; 
}
public void loadYesNo(ValueChangeEvent evt){
Integer yesNoValue = (Integer)evt.getNewValue();
setYesNoRadio(yesNoValue);
userDetailsGridFlag = true;

}


}

推荐答案

您需要将bean放在视图范围内,以便为后续请求保留rendered属性的基础条件.

You need to put the bean in the view scope in order to retain the underlying condition of the rendered attribute for the subsequent requests.

@ManagedBean
@ViewScoped
public class User {
    // ...
}


无关,与具体问题无关,valueChangeListener旨在在您希望在服务器端值更改事件上具有钩子的情况下使用,该事件使您既可以也可以强大>旧的价值和新的价值就在您手中.例如,记录一个事件.并非旨在根据更改执行业务操作.为此,您应该改用<f:ajax>listener属性.


Unrelated to the concrete problem, the valueChangeListener is intended to be used whenever you want to have a hook on the server side value change event which allows you to have both the old value and the new value at your hands. For example, to log an event. It's not intended to perform business actions based on the change. For that you should be using the listener attribute of <f:ajax> instead.

所以,替换

<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}" valueChangeListener="#{user.loadYesNo}">
    <f:selectItem itemValue="1"  itemLabel="YES"></f:selectItem>
    <f:selectItem itemValue="0" itemLabel="NO"></f:selectItem>
    <f:ajax event="change" execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>

使用

<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
    <f:selectItem itemValue="1"  itemLabel="YES"></f:selectItem>
    <f:selectItem itemValue="0" itemLabel="NO"></f:selectItem>
    <f:ajax execute="@form" listener="#{user.loadYesNo}" render="userDetailsGrid"></f:ajax>
</h:selectOneRadio>

并从方法中删除ValueChangeEvent属性.

这篇关于不会从&lt; h:selectOneRadio&gt;中调用valueChangeListener.放置在&lt; h:panelGrid&gt;侧面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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