2

My page:

...
    <div id="header">
       <!-- content header -->
     </div>
     <div id="content">
       <h:messages />
       <h:ouputText value="#{example.text}" />
     </div>
...

My managedBean:

public class ExampleManagedBean(){
       private String text;


       public String getText(){
           FacesContext.getCurrentInstance().
                   addMessage(null, 
                      new FacesMessage(FacesMessage.SEVERITY_WARN, 
                                       "Warning message...", null));
           return text;
       }

       public void setText(String text){
           this.text = text;
       }
   }

My problem is that the warning message not is rendered in page. Why?

4
  • is there an <f:view>? Is the page refreshed? Commented Feb 26, 2010 at 11:23
  • Yes, there is an <f:view>. The page not is refreshed, because it is the first page. Commented Mar 2, 2010 at 8:57
  • @Bozho: Is <f:view> is required for popping out messages for display? Commented Apr 11, 2012 at 18:07
  • not sure anymore :) it has been 2 years since I last used JSF Commented Apr 11, 2012 at 20:00

2 Answers 2

3

I have two ideas.

First, the message is added in the getText() getter. If h:messages is rendered before h:outputText, then the message might not be there yet when h:messages is rendered.

Second, messages disappear on redirects.

Sign up to request clarification or add additional context in comments.

Comments

2

FacesMessage is send to <h:messages/> during validation phase of JSF lifecycle. In your case getter is used to derive bean property value and no validation is going on, so the message is empty.

In theory you can use setter validation, but this is a well known antipattern.

You can do "by hand" validation, but in a different way

 <div id="content">
    <h:messages />
    <h:form>
        <h:outputText value="#{example.text}" />
        <h:commandButton value="Click" action="#{example.action}"/>
    </h:form>
 </div>

and the action method is

public String action(){
   FacesContext.getCurrentInstance().
        addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_WARN,
                                       "Warning message...", null));
           return null;
}

Much cleaner approach would use built-in or custom validator.

2 Comments

Sorry, but you're completely wrong. The by OP posted code is valid and supposed to work. The FacesMessages are derived during renderresponse, regaredless of where they're been set. The problem lies somewhere else, the OP has yet to clarify it based on the comment of Bozho. Until then it's only shooting in the dark.
Yes, there is an <f:view>. The page not is refreshed, because it is the first page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.