Question

I was just wondering to implement a kind of log display to user where in all messages in the application are displayed to user all the time.

Since I use JSF 1.2 and RF 3.3.3, wanted to know if it is possible to save all messages be it added by different requests and display them to user, so that user will know the history of actions he has done. It is also useful for support team to analyse the cause of the problem and he can send the messages to developer if it needs to simulated or to debug purpose also.

I also know facesmessages get cleared over different requests, thats where my question lies, to save messages over different requests :)

Could be handled in different ways by saving them is a session variable or so...I would appreciate all possible answers

Was it helpful?

Solution

You could collect all messages during the render response in a PhaseListener. E.g.

public class MessagesListener implements PhaseListener {

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    @Override
    public void beforePhase(PhaseEvent event) {
        FacesContext context = event.getFacesContext();
        Iterator<String> clientIds = context.getClientIdsWithMessages();

        while (clientIds.hasNext()) {
            String clientId = clientIds.next();
            Iterator<FacesMessage> messages = context.getMessages(clientId);

            while (messages.hasNext()) {
                FacesMessage message = messages.next();
                save(clientId, message); // Do your job here.
            }
        }
    }

    @Override
    public void afterPhase(PhaseEvent event) {
        // NOOP.
    }

}

To get it to run, register it as follows in faces-config.xml:

<lifecycle>
    <phase-listener>com.example.MessagesListener</phase-listener>
</lifecycle>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top