2016-08-01 2 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 

Iの値を入力すると、それは、次いで後h:outputText要素に提出示します。 しかし、空の値(間違っている)を入力すると、h:outputText要素には前の値が残っています。

値を入力しなかった場合、 'h:outputText'を非表示にするにはどうすればよいですか?

+0

あなた@ViewScoped注釈 – JokerTheFourth

+0

@JokerTheFourthを変更してみてください:には何、なぜ? – Kukeltje

+0

何かを隠すために、この場合は 'Disabled'や' Rendred'を使うことができます(Rendred)。それは見えなくなります。 –

答えて

2

私は上記のコードで2つの問題を参照してください。

  1. コマンドボタンをクリックすると、h:outputTextが更新されません。あなたはhにIDを追加する必要があります: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 
        } 
    } 
    
関連する問題