2014-10-27 128 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"來修復。