2016-08-01 85 views
1
<h:form id="aform"> 

    <p:growl id="debug-growl" showDetail="true" sticky="false" /> 

    <p:inputText id="expression" value="#{debug.expression}" required ="true" /> 

    <p:commandButton update="debug-growl" value="Process" action="#{debug.myaction}" /> 

    <h:outputText value="Source regular expression: #{debug.expression}" rendered="#{not empty debug.expression}" /> 

</h:form> 

隱藏H:如果的outputText元素沒有用戶輸入

@ManagedBean 
@ViewScoped 
public class Debug implements Serializable { 

    private String expression; //getter & setter is present 

當我進入那說明提交在h:outputText元素後的值。 但是如果我輸入空值(這是錯誤的),那麼在h:outputText元素中仍然存在以前的值。

未提交任何值時如何隱藏'h:outputText'?

+0

試着改變你的@ViewScoped註釋 – JokerTheFourth

+0

@JokerTheFourth:要什麼,爲什麼? – Kukeltje

+0

要隱藏一些你可以使用'Disabled'或'Rendred'的例子(Rendred),它將被隱藏。 –

回答

2

所以我看到2個問題與上述代碼。

  1. 您沒有更新命令按鈕上的h:outputText點擊。你需要一個ID添加到H:的outputText它作爲更新添加到命令按鈕

    <p:commandButton update="debug-growl someText" value="Process" action="#{debug.myaction}" /> 
    
    <h:outputText id = "someText" value="Source regular expression: #{debug.expression}" rendered="#{not empty debug.expression}" /> 
    
  2. 所需=「真」上的inputText是不允許空值將被提交到服務器。因此h:outputText永遠不會是空的,因此這個值總是會被​​渲染。要解決這個問題,我會在服務器上進行驗證。

    JSF

    取出所需= 「true」 標記

    <p:inputText id="expression" value="#{debug.expression}"/> 
    

    的Java

    public void myAction(){ 
    
        //check to make sure inputText isnt null/blank first 
        if(StringUtils.isBlank(expression)) 
         FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error", "Must provide value")); 
        else{ 
         //business logic 
        } 
    } 
    
相關問題