2014-10-27 127 views
0
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正常工作是:呈现未在JSF

<!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上的渲染也不起作用。

回答

0

如wittakarn所示,修改渲染属性,并且您的outputText已正确渲染。

但它永远不会消散,因为如果新选择与“狗”不同,您从未将showOut设置为false。

我的建议是要改变内容冯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; 
    } 
} 
1

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