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 8769905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:27:11+00:00 2026-06-13T17:27:11+00:00

in my preRender code for a page i add faces message then make navigation

  • 0

in my preRender code for a page i add faces message then make navigation to another page as follows:

if(error){
addMessageToComponent(null,"AN ERROR HAS OCCURRED");
FacesContext.getCurrentInstance().getExternalContext().getFlash()
                .setKeepMessages(true);
navigateActionListener("myoutcome");
}

and the util methods for adding message and navigation are:

public static String getClientId(String componentId)
    {
        FacesContext context = FacesContext.getCurrentInstance();
        UIViewRoot root = context.getViewRoot();

        UIComponent c = findComponent(root, componentId);
        return c.getClientId(context);
    }

    public static UIComponent findComponent(UIComponent c, String id)
    {
        if (id.equals(c.getId())) { return c; }
        Iterator<UIComponent> kids = c.getFacetsAndChildren();
        while (kids.hasNext())
        {
            UIComponent found = findComponent(kids.next(), id);
            if (found != null) { return found; }
        }
        return null;
    }

    /**
     * @param componentId
     *            : the id for the jsf/primefaces component without formId:
     *            prefix. <br>
     *            if you use null then the message will be added to the
     *            h:messages component.
     **/
    public static void addMessageToComponent(String componentId, String message)
    {

        if (componentId != null)
            componentId = GeneralUtils.getClientId(componentId);
        FacesContext.getCurrentInstance().addMessage(componentId,
                new FacesMessage(message));
    }

public static void navigateActionListener(String outcome)
    {
        FacesContext context = FacesContext.getCurrentInstance();
        NavigationHandler navigator = context.getApplication()
                .getNavigationHandler();
        navigator.handleNavigation(context, null, outcome);
    }

but messages are not saved and so it doesn’t appear after redirect.

please advise how to fix that.

  • 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-13T17:27:12+00:00Added an answer on June 13, 2026 at 5:27 pm

    The preRenderView event runs in the very beginning of the RENDER_RESPONSE phase. It’s too late to instruct the Flash scope to keep the messages. You can do this at the latest during the INVOKE_APPLICATION phase.

    Since there’s no standard JSF component system event for this, you’d need to homebrew one:

    @NamedEvent(shortName="postInvokeAction")
    public class PostInvokeActionEvent extends ComponentSystemEvent {
    
        public PostInvokeActionEvent(UIComponent component) {
            super(component);
        }
    
    }
    

    To publish this, you need a PhaseListener:

    public class PostInvokeActionListener implements PhaseListener {
    
        @Override
        public PhaseId getPhaseId() {
            return PhaseId.INVOKE_APPLICATION;
        }
    
        @Override
        public void beforePhase(PhaseEvent event) {
            // NOOP.
        }
    
        @Override
        public void afterPhase(PhaseEvent event) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getApplication().publishEvent(context, PostInvokeActionEvent.class, context.getViewRoot());
        }
    
    }
    

    After registering it as follows in faces-config.xml:

    <lifecycle>
        <phase-listener>com.example.PostInvokeActionListener</phase-listener>
    </lifecycle>
    

    You’ll be able to use the new event as follows:

    <f:event type="postInvokeAction" listener="#{bean.init}" />
    

    You only need to make sure that you’ve at least a <f:viewParam>, otherwise JSF won’t enter the invoked phase at all.

    The JSF utility library OmniFaces already supports this event and the preInvokeAction event out the box. See also the showcase page which also demonstrates setting a facesmessage for redirect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know any drawback to add controls to a page on PreRender event?
I am trying to add controls to the page from the code behind in
I am attempting to use the code-behind (Page_Load or PreRender) to set a date-time
On the PreRender event of my page, I decide to do a response.redirect() .
I have an asp.net web page. The code: <%@ Page Language=C# AutoEventWireup=true CodeBehind=Login.aspx.cs Inherits=Works.Login
I'm using the code below to render a preview of a PDF page. However
I have a Page (form) that has many UserControls on it, and I'm trying
here is the Manual and the Code of my again trying to make a
I have 3 ASPX pages. A usercontrol on each page has an email placeholder
I want to add options to a dropdownbox on my aspx page from the

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.