How to emulate the behavior of a request scope in a bean session scope?

In my jsf application, I have a mail send button. And every time he clicked, I want to show a message that the mail was or was not sent.

This is typical query functionality. But the problem is that I have 1 bean support with session scope. And all the data is in this bean. And the 'send' method specified by the button action attribute is in that bean.

So what is the solution? If I have to create another bean scope request, then how should I refer to it from my bean session?

+3
source share
2 answers

, FacesMessage , , FacesContext#addMessage(). FacesMessages , , , .

bean:

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);
}

null clientId "", :

 <h:messages globalOnly="true" />

:, , 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);

.. infoClass/infoStyle errorClass/errorStyle h:messages:

 <h:messages globalOnly="true" infoStyle="color:green" errorStyle="color:red" />
+3

:

  • send ( ExternalContext)
  • bean ( bean )
+1

Source: https://habr.com/ru/post/1730849/


All Articles