Is Struts2-Full-Hibernate plugin the standard way

2019-01-20 07:27发布

问题:

I'm starting a project, willing to use Struts2 and Hibernate.

Should I use the struts2-full-hibernate plugin, or integrate them differently ?

Searching on Internet confused me: is it the standard way to integrate them ? If not, which is the standard way ?

回答1:

In a nutshell:

  1. Choose a framework for the front-end (usually MVC, then Struts2, JSF2, Spring MVC, etc... you've already chosen Struts2. The standard (not necessarily the better nor the most used) in the Java EE 6+ stack is JSF2);
  2. Choose a persistence manager:

    • the standard with Java EE 6+ is JPA 2.0 (JSR 317 - Java Persistence API). JPA are just annotations, you need a library implementing them; Hibernate can be used as JPA implementation. Hibernate is not the only JPA provider, but it is the most used one (not necessarily the best one), and hence the most standard. With this configuration, you can structure the application's layers by separating the presentation layer (Struts2 actions) from the persistence layer, where the CRUD is performed. The DAO layer is also not needed anymore because JPA's EntityManager is the dao itself.

    • You can otherwise use raw Hibernate with its proprietary annotations (or any other persistence manager), and in that case, with Struts2, you can use the (vintage?) Struts2-Full-Hibernate plugin. It simplifies some jobs, but force you to use the OSIV (Open-Session-In-View) (anti)pattern.

  3. After having chosen the framework and the persistence manager, you need to chose a DI (Dependency Injection) manager. If you are using Java EE 6+, the standard is to use CDI (JSR 299 - Contexts and Dependency Injection). Before Java EE 6, or for nostalgic developers, Spring is still available. It's been the first library providing DI / IoC (Inversion of Control) when Java EE was lacking it.

    Specifically, with Struts2 you can:

    • integrate CDI with the Struts2-CDI-plugin;
    • integrate Spring with the Struts2-Spring-plugin.

Conclusion

According to Java EE, the standard configuration with Struts2 (instead of JSF2) is:

  • Struts2
  • Java EE 6+ (CDI + JPA 2.x + EJB 3.x)
  • Hibernate 4.x
  • Struts2-CDI-plugin