Java EE - 在将域模型对象传递给视图之前格式化域模型对象的正确层是什么?
我正在开发一个具有典型分层架构的 Web 应用程序:
DAO 层从数据库检索域模型对象;
该层与服务层通信,服务层使用这些对象执行一些业务操作;
Web 层(Spring 控制器)使用服务层检索域模型对象(或它们的集合)并将它们传递到视图层;
视图层要么是使用 JSTL 显示数据的简单 JSP,要么是通过 AJAX 以 JSON 对象(通过 Jackson 库将域对象转换为 JSON)形式检索一些数据的 JSP。
我一直在研究以下内容:
我经常需要将数据库字段转换为不同的格式以向用户显示。例如,日期可能存储为时间戳,我希望它将其显示为格式化日期(例如 dd/mm/yyyy)。
另外,我需要做相反的事情,将一些值(通常是用户输入)转换为域模型对象属性的格式。
我的问题是,我应该在哪里进行这种转换?特别是对于 JSON 数据,它们应该已经在 AJAX 响应上进行格式化,我认为我不应该使用 Javascript 对其进行格式化,对吗?
先感谢您。
I am developing a web application which has a typical layered architecture:
a DAO layer that retrieves domain model objects from a database;
this layer communicates with the service layer which does some business operations using those objects;
the web layer (Spring Controllers) use the service layer to retrieve the domain model objects (or collections for them) and pass them to the view layer;
the view layer is either simple JSPs which show the data using JSTL, or JSPs which retrieve some of the data through AJAX in the form of JSON objects (domain objects converted to JSON through Jackson library).
I have been researching about the following:
Very often I need to convert the db fields to a different format to show to the user. For example, a Date might be store as a Timestamp and I want it to show it as a formatted date (e.g. dd/mm/yyyy).
Also, I need to do the opposite, convert some value (usually user input) to the format of a domain model object's property.
My question is, where should I be doing this kind of conversions? Especially with JSON data, they should be already formatted on the AJAX response, I dont think I should format it with Javascript, am I right?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的观点是,所有数据格式转换都应该在视图或协作对象中完成。模型或服务层不应执行此活动。在模型中执行此操作将是糟糕的设计,因为它将模型公开的接口仅耦合到一个视图。此外,它会使视图中的更改变得更加困难,也需要更改模型。
您可以参考编写良好的 JSF 应用程序(因为您提到您正在使用 Java EE),其中编写了特定的转换器以确保视图能够正确格式化域对象的内容。
这取决于您如何看待视图。如果您对视图的表示是服务器必须向客户端提供格式化数据,那么服务器应该这样做。但是,大多数应用程序都采用实用的方法,其中客户端逻辑也被视为视图的一部分。根据使用数据的上下文,您可以:
My opinion is that all data format conversions ought to be done in the view, or in collaborative objects. The model or the service layer should not be performing this activity. It would be poor design to perform this in the model, for it would couple the interfaces exposed by the model to only one view. Also, it would make it more difficult to make changes in the view, requiring changes in your model as well.
You can refer to well-written JSF applications (since you mentioned that you are using Java EE), where specific converters are written to ensure that the view would format the contents of the domain objects appropriately.
That would depend on how you view the view. If your representation of the view is that the server must provide the client with formatted data, then the server ought to do this. But, most applications take the practical approach where in client-side logic is also treated as part of the view. Depending on the context in which the data is used, you may: