tags:

views:

156

answers:

2

In my jsf application I have a button for sending mail. And each time it clicked I want to show message that mail was or wasn't sent.

This is a typical request-scope functionality. But the problem is that I have 1 backing bean with session scope now. And all data is in this bean. And method 'send' referred by action attribute of the button is in this bean.

So, what is the way out? If I should create one more request-scope bean then how should I refer to it from my session bean?

+1  A: 

Either:

  • just push the message onto the request scope from the send method (via the ExternalContext)
  • refactor the method to a request-scope bean and inject any session information it needs (the managed bean framework can do this)
McDowell
+3  A: 

Another approach, you can make use of FacesMessage here which you add to the context using FacesContext#addMessage(). FacesMessages are request based and likely more suited for the particular functional requirement than some custom messaging approach.

Here's an example of the bean action method:

public void sendMail() {
    FacesMessage message;
    try {
        Mailer.send(from, to, subject, message);
        message = new FacesMessage("Mail successfully sent!");
    } catch (MailException e) {
        message = new FacesMessage("Sending mail failed!");
        logger.error("Sending mail failed!", e); // Yes, you need to know about it as well! ;)
    }
    FacesContext.getCurrentInstance().addMessage(null, message);
}

With a null clientId the message becomes "global", so that you can make use of the following construct to display only global messages:

 <h:messages globalOnly="true" />

Update: to have the success and error message displayed in a different style, play with the FacesMessage.Severity:

        message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Mail successfully sent!", null);
    } catch (MailException e) {
        message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Sending mail failed!", null);

.. in combination with infoClass/infoStyle and errorClass/errorStyle in h:messages:

 <h:messages globalOnly="true" infoStyle="color:green" errorStyle="color:red" />
BalusC
I've already found a workaround. I have a request-scope bean and I pass my session-scoped bean to it with propertyActionListener. Request-scoped bean calls send() method in session-scoped bean. Your solution is obviously better because it's even difficult to explain mine.
Roman
It's clear enough, I already understood it when you said propertyActionListener.
BalusC