0

I am trying to conditionally display a PrimeFaces message but it is not working. My controller PostConstruct method looks like this:

@ManagedBean
@ViewScoped
public class DIGRCController {

    private DIGRC digrc;

    private DIGRCService service;

    //getters and setters

    @PostConstruct
    public void init() {

        digrc = new DIGRC();

        service = new DIGRCService();

        if(digrc.getIsValueSet()) 
        {
            FacesContext.getCurrentInstance().addMessage(
                "messages",
                new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning!", "ERROR! Can't updated."));
        }

    }
}

If some condition is met after constructing the bean, I want to display a warning message on top of my form. My view looks like this:

<h:form id="mainForm">

    <p:messages for="messages" autoUpdate="true" />

    <!-- other content -->

</h:form>

The condition in place is been met (I tested that) but the message is not been displayed. Why is this happening?

I am using PrimeFaces 5.3 and JSF 2.2.

Erick
  • 823
  • 16
  • 37
  • 1
    http://stackoverflow.com/questions/10197277/how-can-i-add-faces-messages-during-postconstruct – Jaqen H'ghar Apr 11 '16 at 05:48
  • Which component id is `messages` which u added in `` – Subodh Joshi Apr 11 '16 at 06:04
  • @BalusC in this case, I can't use because I need the bean to be constructed and after the bean is constructed, I need to display the message. To display the message, I need the bean because I need to check some bean property. If that bean property is true, then display message. How can I accomplish that? – Erick Apr 17 '16 at 17:45
  • Are you saying that the bean isn't constructed when you use ``? – BalusC Apr 17 '16 at 17:47
  • That's what I think. I am doing: and I am getting an error saying Method not Found ... So I am assuming is because occurs before the bean is constructed? Is that assumption correct? – Erick Apr 17 '16 at 17:51
  • Well, I'd say the exception is clear. There's no such method on the bean. Perhaps you need to clean/rebuild/etc. Or you need to verify the method signature (just a public void method which can optionally take `ComponentSystemEvent` argument). – BalusC Apr 17 '16 at 18:35
  • The question is: does the event preRenderView occurs after the bean is constructed? Put in other way: does the event preRenderView occurs after the method @PostConstruct is executed? If yes, then I am doing something wrong. – Erick Apr 17 '16 at 18:42
  • As usual, the bean is only constructed when it does not exist yet at the moment it is accessed. The exception is evident that you did something wrong. If the bean was not found at all (nor constructed), you would have gotten a `PropertyNotFoundException: Target unreachable` instead and EL wouldn't even have attempted to look for the method. – BalusC Apr 17 '16 at 19:59

0 Answers0