在JSF和JPA项目中使用POJO作为模型,对吗? [英] Using POJO as Model in JSF and JPA project, is that right?

查看:64
本文介绍了在JSF和JPA项目中使用POJO作为模型,对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2和JPA 2(EclipseLink 2.3)开发一个项目. 在JSF 2中,我了解到必须将Model与Controller分开,并将同一事物与View分开(感谢BalusC). 但是现在使用从JPA生成的POJO,我想知道现在的bean(模型)是否应该是pojos.

i'm developing a project using JSF 2 and JPA 2 (EclipseLink 2.3). In JSF 2 I learned that we must separate the Model from Controller and the same thing to the View (thanks BalusC). But now with the POJO's generated from JPA, I wonder if the bean, the model, it should be the pojos now.

我的视图将是用JSF 2.0开发的.xhtml页面,它将与我的控制器进行交互,然后在控制器中调用DAO的类,然后在我的数据库中进行操作.

My view is gonna be my .xhtml pages, developed in JSF 2.0, that will interact with my controllers then in controllers call the DAO's classes and then operate in my database.

这是对的吗?我的意思是MVC的概念吗? 我想做正确的事,有提示吗?

Is this right ? I mean in the concept of MVC ? I want to do everything right, any tip ?

谢谢.

推荐答案

"MVC"在JSF中具有多个观点.从高级的角度来看,该模型由EJB/JPA以及最终的DAO/DTO(如果有)表示.该视图由您自己的JSF代码(由托管Bean和Facelets模板组成)表示.控制器由FacesServlet表示.

"MVC" has in JSF multiple points of view. From the high level view, the Model is represented by EJB/JPA and eventually DAO/DTO (if any). The View is represented by your own JSF code (which consits of managed beans and Facelets templates). The Controller is represented by the FacesServlet.

从低级视图(高级视图的进一步细分)中,模型由实体或DTO表示.视图由Facelets模板表示. Controller由您的托管bean表示.基本上是M(MVC)C.

From low level view (a further subdivision of the high level View), the Model is represented by entities or DTOs. The View is represented by your Facelets templates. The Controller is represented by your managed bean. It's basically a M(MVC)C.

请注意,"POJO"是旧的J2EE/Hibernate时代的一个相当古老的术语.如今,在Java EE/JPA中,它们被称为实体".是的,它们是那些@Entity Javabeans. Als注意,有些人可能会选择使用普通的DTO而不是实体,这些实体应该将JSF代码与服务层分离.然后,JSF应该将这些DTO用作模型,并且服务层应该依次在这些DTO和真实实体之间进行映射.我认为这是没有必要的. EJB3/JPA2是一个非常漂亮的API,它已经使您可以像过去的J2EE时代那样使用DAO/DTO的许多样板代码减到最少.使用Java EE 6和更高版本,实际上并不需要能够将服务层切换到例如Spring.一切都已经经过深思熟虑并标准化了.

Note that "POJO" is a rather legacy term from the old J2EE/Hibernate times. Nowadays, with Java EE/JPA, they're called "Entities". Yes, it are those @Entity Javabeans. Als note that some may opt to use plain vanilla DTOs instead of entities which should decouple your JSF code from the service layer. JSF should then use those DTOs as model and the service layer should in turn map between those DTOs and the real entities. This is in my opinion not necessary. EJB3/JPA2 is a pretty slick API which already minimizes a lot of boilerplate code for which you would have used DAOs/DTOs like as in old J2EE ages. With Java EE 6 and higher, there's not really a need to be able to switch of service layer to for example Spring. Everything is already well thought out and standardized.

  • What components are MVC in JSF MVC framework?
  • Difference between DTO, VO, POJO, JavaBeans?
  • I found JPA, or alike, don't encourage DAO pattern

这篇关于在JSF和JPA项目中使用POJO作为模型,对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆