数据模型中的 Spring Formatters,这是否违反了 MVC?有更好的选择吗?
Spring 通过使用注释提供格式化程序(和转换器)。这意味着可以在控制器上注释请求参数以格式化用户输入,并且还可以注释数据模型类的属性以格式化视图的数据。
在我看来,后者明显违反了 MVC 的主要目的,即模型和视图之间的分离。使用格式细节注释数据模型类会将模型绑定到视图。如果模型必须用于其他视图或其他任何内容,则它不能,因为它的字段是针对特定视图进行格式化的。
如果我错了请告诉我。如果没有,是否有一种方法可以在不违反 MVC 的情况下格式化字段并查看格式?
Spring provides formatters (and converters) with the use of annotations. This means that a request parameter can be annotated on a controller to format user input and also a property of a data model class can be annotated to format data for a view.
The latter seems to me as a clear violation of MVC's main purpose, that is the separation between model and view. Annotating a data model class with formatting specifics binds the model to the view. If the model hast to be used for some other view or for anything else, it can't, since its fields are formatted for a specific view.
If I am wrong let me know. If not, is there a way to format fields from and to view format without violating MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用注释是指定格式的一种很好的声明方式 - 我不会放弃它。另一种选择是定义转换的程序方式。
在我看来,问题在于纯模型对象和表单支持对象混合在一起。如果您想“净化”您的架构,请在用户输入和模型之间引入表单支持对象。如果您认为它对于您的项目规模来说太复杂,那么您就不需要它。在这种情况下,请注意模型对象具有双重含义。
Using annotations is a good declarative way to specify formatting - I would not give it up. An alternative would be a procedural way of defining conversions.
As I see it, the problem is that pure model objects and form-backing objects get mixed together. If you want to "purify" your architecture, introduce form-backing objects that will be between user's input and your model. If you think that it is too complex for the scale of your project, then you don't need it. In this case, just be aware that the model object has a double meaning.