Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:07:42+00:00 2026-06-09T18:07:42+00:00

I have the following commandButton action method handler: public String reject() { //Do something

  • 0

I have the following commandButton action method handler:

public String reject()
{
  //Do something
  addMessage(null, "rejectAmountInvalid", FacesMessage.SEVERITY_ERROR);

  redirectToPortlet("/xxx/inbox?source=pendingActions#pendingApproval");
}

public static void addMessage(String clientId, String key, Severity level, Object... objArr)
{
    FacesContext context = FacesContext.getCurrentInstance();
    FacesMessage message = null;
    String msg = getTextFromResourceBundle(key);
    if (objArr != null && objArr.length > 0)
        msg = MessageFormat.format(msg, objArr);
    message = new FacesMessage(msg);

    message.setSeverity(level);
    context.addMessage(clientId, message);
}

public static void redirectToPortlet(String urlToRedirect)
{
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext externalContext = context.getExternalContext();

    try
    {
        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
        ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute("THEME_DISPLAY");
        String portalURL = themeDisplay.getPortalURL();
        String redirect = portalURL + urlToRedirect;
        externalContext.redirect(redirect);
    }
    catch (Throwable e)
    {
        logger.log("Exception in redirectToPortlet to the URL: " + urlToRedirect, VLevel.ERROR, e);
    }
}

When the page is redirected to “/xxx/inbox?source=pendingActions#pendingApproval”, the error message I added is lost. Is there a way to preserve the error message in JSF 2.1?

Thanks
Sri

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:07:44+00:00Added an answer on June 9, 2026 at 6:07 pm

    You can use a PhaseListener to save the messages that weren’t displayed for the next request.

    I’ve been using for a while one from Lincoln Baxter’s blog post Persist and pass FacesMessages over multiple page redirects, you just copy the class to some package and register on your faces-config.xml.

    It’s not mentioned explicitly in the blog post, but I’m assuming the code is public domain, so I am posting here for a more self-contained answer:

    package com.yoursite.jsf;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.context.FacesContext;
    import javax.faces.event.PhaseEvent;
    import javax.faces.event.PhaseId;
    import javax.faces.event.PhaseListener;
    
    /**
     * Enables messages to be rendered on different pages from which they were set.
     *
     * After each phase where messages may be added, this moves the messages
     * from the page-scoped FacesContext to the session-scoped session map.
     *
     * Before messages are rendered, this moves the messages from the
     * session-scoped session map back to the page-scoped FacesContext.
     *
     * Only global messages, not associated with a particular component, are
     * moved. Component messages cannot be rendered on pages other than the one on
     * which they were added.
     *
     * To enable multi-page messages support, add a <code>lifecycle</code> block to your
     * faces-config.xml file. That block should contain a single
     * <code>phase-listener</code> block containing the fully-qualified classname
     * of this file.
     *
     * @author Jesse Wilson jesse[AT]odel.on.ca
     * @secondaryAuthor Lincoln Baxter III lincoln[AT]ocpsoft.com 
     */
    public class MultiPageMessagesSupport implements PhaseListener
    {
    
        private static final long serialVersionUID = 1250469273857785274L;
        private static final String sessionToken = "MULTI_PAGE_MESSAGES_SUPPORT";
    
        public PhaseId getPhaseId()
        {
            return PhaseId.ANY_PHASE;
        }
    
        /*
         * Check to see if we are "naturally" in the RENDER_RESPONSE phase. If we
         * have arrived here and the response is already complete, then the page is
         * not going to show up: don't display messages yet.
         */
        // TODO: Blog this (MultiPageMessagesSupport)
        public void beforePhase(final PhaseEvent event)
        {
            FacesContext facesContext = event.getFacesContext();
            this.saveMessages(facesContext);
    
            if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId()))
            {
                if (!facesContext.getResponseComplete())
                {
                    this.restoreMessages(facesContext);
                }
            }
        }
    
        /*
         * Save messages into the session after every phase.
         */
        public void afterPhase(final PhaseEvent event)
        {
            if (!PhaseId.RENDER_RESPONSE.equals(event.getPhaseId()))
            {
                FacesContext facesContext = event.getFacesContext();
                this.saveMessages(facesContext);
            }
        }
    
        @SuppressWarnings("unchecked")
        private int saveMessages(final FacesContext facesContext)
        {
            List<FacesMessage> messages = new ArrayList<FacesMessage>();
            for (Iterator<FacesMessage> iter = facesContext.getMessages(null); iter.hasNext();)
            {
                messages.add(iter.next());
                iter.remove();
            }
    
            if (messages.size() == 0)
            {
                return 0;
            }
    
            Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
            List<FacesMessage> existingMessages = (List<FacesMessage>) sessionMap.get(sessionToken);
            if (existingMessages != null)
            {
                existingMessages.addAll(messages);
            }
            else
            {
                sessionMap.put(sessionToken, messages);
            }
            return messages.size();
        }
    
        @SuppressWarnings("unchecked")
        private int restoreMessages(final FacesContext facesContext)
        {
            Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
            List<FacesMessage> messages = (List<FacesMessage>) sessionMap.remove(sessionToken);
    
            if (messages == null)
            {
                return 0;
            }
    
            int restoredCount = messages.size();
            for (Object element : messages)
            {
                facesContext.addMessage(null, (FacesMessage) element);
            }
            return restoredCount;
        }
    }
    

    And then, on your faces-config.xml:

    <phase-listener>com.yoursite.jsf.MultiPageMessagesSupport</phase-listener>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following layout in my page (simplified) <h:form> <h:commandButton action=#{bean.save} value=Save/> <rich:tabPanel
I have following two methods in my backing bean - public String validateUser() {
I have the following code inside a method invoked by a jsf <h:commandButton>. It
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have the following scenario (using rich faces): ... <a4j:outputPanel id=foo> <h:commandButton disabled=#{not _user.selected}
I have following sample code. Initially, only commandButton Two is visible. When I click
In my code, I have the following objects to implement a shopping cart: public
I have the following JSF table: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I have the following backing bean: @ViewScoped @ManagedBean public class WeighFamilyBacking2 implements Serializable {
I have the following page and somehow the <p:commandButton value=Save update=data oncomplete=edit.hide(); actionListener=#{dataBean.update} />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.