Java EE - 在将域模型对象传递给视图之前,格式化域模型对象的正确层是什么? [英] Java EE - What is the correct layer for formatting domain model objects before passing them to views?

查看:140
本文介绍了Java EE - 在将域模型对象传递给视图之前,格式化域模型对象的正确层是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有典型分层架构的Web应用程序:

I am developing a web application which has a typical layered architecture:

从数据库中检索域模型对象的DAO层;

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;

网络层( Spring Controllers)使用服务层来检索域模型对象(或它们的集合)并将它们传递给视图层;

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;

视图层是简单的JSPs使用JSTL或JSP以JSON对象的形式通过AJAX检索一些数据(通过Jackson库转换为JSON的域对象)来显示数据。

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:

我经常需要将数据库字段转换为不同的格式以向用户显示。例如,Date可能存储为时间戳,我希望它将其显示为格式化日期(例如dd / mm / yyyy)。

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.

我的问题是,我应该在哪里做这种转换?特别是对于JSON数据,它们应该已经在AJAX响应上格式化了,我不认为我应该用Javascript格式化它,我是对的吗?

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?

提前谢谢你。

推荐答案


我经常需要将db字段转换为另一种格式为
show给用户。例如,Date可能存储为时间戳
,我希望它将其显示为格式化日期(例如dd / mm / yyyy)。

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?

我认为所有数据格式转换都应该在视图中完成,或者在协作对象中。模型或服务层不应执行此活动。在模型中执行此操作将是糟糕的设计,因为它会将模型公开的接口耦合到仅一个视图。此外,它会使在视图中进行更改变得更加困难,同时也需要更改模型。

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.

您可以参考编写良好的JSF应用程序(因为您提到您正在使用Java EE),其中编写特定的转换器以确保视图将适当地格式化域对象的内容。

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.


特别是对于JSON数据,它们应该已经在AJAX响应上格式化了,我不认为我应该用Javascript格式化它,我是对的吗?

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?

这取决于您查看视图的方式。如果您对视图的表示是服务器必须为客户端提供格式化数据,那么服务器应该这样做。但是,大多数应用程序采用实际方法,其中客户端逻辑也被视为视图的一部分。根据使用数据的上下文,您可以:

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:


  • 格式化服务器上​​的数据,特别是如果格式化的数据是无需进一步修改即可在客户端普遍使用。

  • 或以规范形式将数据传输到客户端,然后在使用和/或使用数据的上下文中对数据进行格式化显示给用户。

这篇关于Java EE - 在将域模型对象传递给视图之前,格式化域模型对象的正确层是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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