我面临 h:selectOneRadio 的 valueChangeListener="#{user.loadYesNo}"的问题
(我在 Tomcat-7 上使用 Mojarra 2-0-8)。
如果我同时删除了包含“h:selectOneRadio”的 panelGrid,则值更改 litener 将被触发。

看法:

<!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>

模型+ Controller 混合:
    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 放入 View 范围,以便为后续请求保留 rendered 属性的底层条件。

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

与具体问题无关的 valueChangeListener 旨在在您希望在服务器端值更改事件上有一个钩子(Hook)时使用,它允许您拥有 旧值和新值。例如,记录事件。它不打算根据更改执行业务操作。为此,您应该改用 listener<f:ajax> 属性。

所以,更换
<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 属性。

关于jsf-2 - 没有从 <h :selectOneRadio> which is placed in side a <h:panelGrid> 调用 valueChangeListener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9180938/

10-16 07:13