Question

Implementing a simple Login screen using JSF and Spring and Hibernate. I have written the Service and Data Layer Beans in Spring and integrated them with Hibernate.

I defined a Sign Up (new User creation) screen with two fields user id, password in JSF and wired them to a Managed Bean. (Bean Name: Users) Here this bean is also the domain class.

Now on click of the create button in JSF view I need to call the Service Bean methods (which are spring beans). For this I see that I have two ways to do,

  1. Write a method in Users managed bean that takes the given user, password and calls the spring service bean methods which in turn calls DAO bean methods for saving data in DB. But here my question is how far it is a good practise to write controller kind of method in Domain classes?

  2. Second way is to define a new Managed Bean that has the Spring Service object as a property (Which is injected using spring+jsf integration) and a method to call the service bean methods.

Am I doing a correct design? Any thing wrong? Please suggest me for a better design.

Thanks

Was it helpful?

Solution

Dont make your domain class as jsf managed bean.

Generally what I follow is I encapsulate domain class and other UI supporting properties in a form bean(when scenario is complex else direct entity as a property in managed bean) and have it in managed bean.

Spring service is injected in managed bean and on action form bean/entity bean is passed to spring service for business/use case processing and persistence(dao/repository).

Template Code:

@ManagedBean
public class Bean{

  private Entity entity;//or
  private FormBean formBean;

  @Inject private Service service;

  public String doAction(){
    //error processing from service layer and UI message handling
    service.process(entity);//or
    service.process(formBean);
    return Navigation.Constant;
  }
}  

Managed bean purpose should be to collect view data and pass it to service for processing. If you make your domain/entity class as managed bean you will be coupling it with JSF library which is not good for reusability. As per design principle SRP(single responsibility principle), class should have one responsibility in that case it will have more and hence as mentioned above not good practice.

Point 2 as mentioned by you is better.

Hope this helps !!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top