简体   繁体   中英

Suggest a design pattern for application using JSF , EJB 3.0

i have created a demo application Using JSF & EJB 3.0 (Stateless session bean and JPA), my persistence provider is Hibernate 4, and database is Apache Derby.

My class flow ie sequential flow is as follows,

ManagedBean calls Stateless Session bean, in this we have a JPA calls,

please follow the code, the JSF managed bean StudentMgBean.java,

@ManagedBean(name="stMgBean")
@ViewScoped
public class StudentMgBean implements Serializable{
     private static final long serialVersionUID = 109117543434170143L;
     ...........
     @EJB
     private StudentService studentService; 
     .........
     @PostConstruct
     public void init(){
      ..........
      ........
           this.totalStudentInDB = studentService.getMaxStudent();
     }
}

My EJB Interface StudentService.java,

@Local
public interface StudentService {
    List<StudentVO> fetchStudentListOrderByStudentId(boolean flag);

    List<StudentVO> fetchStudentListOrderByStudentName(boolean flag);

    void saveStudentEntity(StudentEntity studentEntity,Integer deptId) throws Exception;

    List<DeptEntity> fetchAllDept();

    List<StudentVO> fetchStudentByDept(Integer deptId);

    void saveAllStudents(List<StudentVO> students) throws Exception;

    void deleteAllStudents(List<StudentVO> students) throws Exception;

    List<StudentVO> fetchStudentListPerPage(Integer minRow,Integer maxRow) throws Exception;

    Integer getMaxStudent() throws Exception;
}

My EJB Stateless Session bean StudentServiceBean.java,

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class StudentServiceBean implements StudentService{
    @PersistenceContext(unitName="forPractise")
    private EntityManager entityMgr;

    @Resource
    private SessionContext sessionContext;

    @EJB
    private DeptService deptService;

    @Override
    public List<StudentVO> fetchStudentListOrderByStudentId(boolean flag){
        .........
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void saveStudentEntity(StudentEntity studentEntity,Integer deptId) throws Exception{
        ........
    }

}

In the StudentServiceBean, i have injected EntityManager, so i directly do JPA operation in the methods written in this session bean.

No my question is can i use any design pattern in this flow, can i go for a separate DAO layer, as i am using EJB 3.0 i don't have to use ServiceLocator pattern, but than any other pattern can i use to seperate Bussiness logic with the JPA call,

One more thing, In JSF managed Bean i have properties and its getter setter methods which are mapped to the JSP componenets in EL like this value={stMgBean.studentList}

but in the same managed bean i have also have method which will be invocked by action command call from JSF, should those method be written in the seperate Managed bean ?

Please suggest a Design pattern which can be used for projects which have JSF 2.0, EJB 3.0 and JPA

Waiting for the reply

You can split the JSF layer using the next concepts:

  • Put all the data which are going to be shared between the java side and the view into specific managed beans called "Models". Now you can manage the scope of the data independently from the scope of the rest of managed beans.
  • Use the command pattern which delegates all the actions which will modified the model to the commands. The commands may invoke the EJB layer, or just update the models without going to the next layer.
  • Keep in the managed beans just the logic that you need to initialise the JSF components in the views or manage their behaviour, a reference to the model, and a reference to a delegator which will provide the command to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM