渲染在JSF上无法正常工作

时间:2014-10-27 10:23:27

标签: jsf-2

My class is as follows:

public class TestBean implements Serializable{
private int id;
private String name;
private boolean showOut;
public TestBean(){
    showOut=false;
}
public void submit(){
    System.out.println("id-----"+id);
}
public void whatsTheName(AjaxBehaviorEvent e){
    System.out.println("listener called");
    if(id==0){
        name="Dog";
        showOut=true;
    }

    else if(id==1)
        name="Cat";
    else
        name="Bird";

}
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    System.out.println("name called-----"+name);        
    return name;
}

public void setName(String name) {
    this.name = name;
}
public boolean isShowOut() {
    System.out.println("showOut called----"+showOut);
    return showOut;
}
public void setShowOut(boolean showOut) {
    this.showOut = showOut;
}
}

和xhtml是:

<!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:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
 <h:form id="frm1">
    <h:selectOneMenu value="#{testBean.id}">
        <f:selectItem itemLabel="Dog" itemValue="0"></f:selectItem>
        <f:selectItem itemLabel="Cat" itemValue="1"></f:selectItem>
        <f:selectItem itemLabel="Bird" itemValue="2"></f:selectItem>
        <f:ajax execute="@form" event="change" 
        listener="#{testBean.whatsTheName}" render=":frm2:out"></f:ajax>
    </h:selectOneMenu>      
</h:form>
<br></br>
<br></br>
<h:form id="frm2">
    <h:outputText id="out" value="#{testBean.name}" rendered="#{testBean.showOut}"/>            
</h:form>

我只想在选择“狗”时显示“输出”框。但是,即使在辅助bean上正确设置了特定变量的值,在outputText上呈现也不起作用。

2 个答案:

答案 0 :(得分:1)

您可以通过渲染整个frm2 render=":frm2"来修复。

答案 1 :(得分:0)

根据wittakarn的建议,修改渲染属性并正确呈现outputText

但它永远不会消失,因为如果新的选择与&#34; Dog&#34;不同,你永远不会将showOut设置为false。

我的建议是更改内容von whatsTheName(...),如下所示:

public void whatsTheName(AjaxBehaviorEvent e) {
    System.out.println("listener called");
    // change showOut in any case
    showOut = (id == 0);
    // apply text accordingly to the selected id
    switch (id) {
    case 0:
        name = "Dog";
        break;
    case 1:
        name = "Cat";
        break;
    case 2:
        name = "Bird";
        break;
    }
}