2

I am working on JSF 2.0, Richfaces 4.0. I am having an requirement where I need to show error message when there is any backend exception occurs. We are displaying a list of users using rich:dataTable. While getting the users list if there is any back end exception occurs, then I need to show error message on the top.

In the backing bean, we are having one variable usersList. In the getUsersList() method, we are calling the db in order to get the users list.

<rich:dataTable value="#{myBean.usersList}>
</rich:dataTable>

Whenever the exception occurs, I am catching that exception in getUsersList() method and constructing FacesMessage obj and adding that message obj to FacesContext. I am using

<rich:messages />

tag to display the error messages. Bu the error message is not displaying.

Can anyone help me on this?

Thanks in advance.

Raj
  • 21
  • 2

1 Answers1

0

Do the DB interaction job in bean's (post)constructor instead of a getter. If the <rich:messages> is positioned in the view before the <rich:dataTable> and the view is opened by a GET request and/or the getter is doing the job during render response only, it's too late to display the message, simply because the <rich:message> component has already been rendered to the response.

In JSF, getters should contain no business logic. They ought merely to be simple entry points to access bean's properties. They can be called more than once during bean's life. For business job you should be using the (post)constructor or (action)event methods.

public class Bean {

    private List<User> users;

    @EJB
    private UserService userService;

    @PostConstruct
    public void init() {
        users = userService.list(); // This will be invoked directly after bean's construction and all dependency injections.
    }

    public List<User> getUsers() {
        return users; // Look, just return the property. Do not do anything else.
    }

    // ...
}

Related:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555