gpt4 book ai didi

jsf-2 - 没有从 which is placed in side a 调用 valueChangeListener

转载 作者:行者123 更新时间:2023-12-01 01:21:04 27 4
gpt4 key购买 nike

我遇到了 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)时使用,它允许您拥有 两个旧值(value)和新值(value)尽在掌握。例如,记录事件。它不打算根据更改执行业务操作。为此,您应该使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com