1

I'm trying to display success message in xhtml page from servlet but no luck, here's my code In servlet i have

FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
            LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 
            Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); 
            FacesContext facesContext = contextFactory.getFacesContext(request.getSession().getServletContext(), request, response, lifecycle); 
            facesContext.addMessage( "user:displaymessagesave", new FacesMessage("user saved successfully" ));

In xhtml page inside form i have written tag for display message form id=user

<h:message class="success" for="displaymessagesave" id="displaymessagesave" />

Saving to database is happening but its not displaying any message, please suggest where i'm going wrong.

2 Answers 2

1

It is not possible to access FacesContext from a servlet other than FacesServlet.

Recently I faced that same problem, and what I did was, on my servlet, put the messages in the Session:

 List<String> msgs=(List<String>)request.getSession().getAttribute(KEY);
 if (msgs==null)
 {
   msgs=new ArrayList<String>();
 }
 msgs.add("My new message");
 request.getSession().setAttribute(KEY, msgs);

Then, on the xhtml view, I added a call to put these messages on FacesContext, and a growl element:

<h:outputText value="#{managed.prepareMessages()}"/>
<p:growl id="growl" showDetail="false" life="4000" />  

Finally, the prepareMessages method takes them from session and add the messages to FacesContext:

public String prepareMessages()
{
    List<String> msgs = (List<String>)FacesContext.getCurrentInstance()
                            .getExternalContext().getSessionMap().get(KEY);
    if (msgs!=null)
    {
        for(String msg:msgs)
        {
            FacesContext.getCurrentInstance().addMessage(null,
                                                     new FacesMessage(msg));
        }
        FacesContext.getCurrentInstance().getExternalContext()
                                      .getSessionMap().remove(KEY);
    }
    return "";
}

It may not be an elegant solution, but is the only one I found...

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

Comments

0

Don't give the same id for message as the "for" component. If you just want a messageholder without attaching it to a valueholder than you should use the tag.

<h:messages class="success" id="displaymessagesave" />

3 Comments

i tried using only id but still the same its not displaying any message.
Have your tried it with h:messages instead of h:message?
You also have to update the h:messages component to see the message on the UI.

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.