服务层中的 hibernate / spring 验证
---添加(编辑)--
即使您在前端进行验证,您是否应该在服务层进行验证?
例如,struts 有验证功能,因此您可以对其进行多次验证。 (我不是指java脚本验证。)
-
即使您已经在前端进行了验证,您是否在服务层中使用hibernate验证器或spring验证?
(前端验证不必是 hibernate 或 spring 验证)
---added (edit) --
should you do validation at the service layer, even if you validate at the front-end?
eg struts has validations so you can validate it a lot. (I don't mean java script validations.)
--
are you using hibernate validator or spring validations in your service layer even though you have validations already on front-end?
(front-end validation don't have to be hibernate or spring one)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一致的数据是企业应用程序中最重要的事情之一!
所以是的:您需要检查两次:在前端和服务层。
在我们的网络应用程序中,我们在不同级别有不同的检查:
(我不是在谈论不一致的检查,我是在谈论检查的粒度和范围。)
IHO 最好检查所有地方,但这是不可能的。因此,您需要一个一致的政策来检查什么。此策略很大程度上取决于您正在开发的应用程序的类型。
Consistent Data are one of the most important thing in Enterprice Applications!
So yes: you need to check it twice: in the frontend and in the Service layer.
In our web applications we have different checks at different levels:
(I am not talking about inconsistent checks, I am talking about granularity and scope of checks.)
IHO it would be the best to check all and everywhere, but this is not possible. So you need a consistent policy where you check what. And this policy strongly depends on the kind of application your are developing.
我们将它与 wicket 结合使用作为前端框架。您必须考虑到需要分两个步骤进行验证:
我们使用业务验证在检票口内进行验证解决了问题。因此您不必编写两次代码。
we used it in combination with wicket as fronend framework. You have to consider that you need to validate in two steps:
We solved the problem using the business validation to validate within wicket. So you don't have to write code twice.
您的问题不够具体,即它看起来不能解决任何一个问题(这更像是一个民意调查)。
要回答更具体的问题“即使在前端验证,是否应该在服务层进行验证?”,答案是绝对的。几乎所有前端验证,尤其是网络应用程序的验证,都可以被绕过。您的服务应该实施适当的验证以确保它们的一致性,并且不应依赖于用户空间中的验证(如 Javascript 检查),因为它们很容易被击败。
更新:当决定在代码前端(调度程序/控制器)和服务层之间进行验证时,我想说没有硬性规定。我可能会检查我们收到的输入在前端是否合理,并在服务层进行域级验证。
Your question is not specific enough i.e. it doesn't look to solve a single problem (it's more of a poll).
To answer the more specific question of "should you do validation at the service layer, even if you validate at the front-end?", the answer is absolutely. Almost all front-end validation, particularly for web-apps, can be bypassed. You services should implement appropriate validation to ensure that they are consistent and should not rely on validation in the user-space (like Javascript checks) as they are easily defeated.
UPDATE: when deciding to validate between the front-end of the code (the dispatcher/controller) and the service layers, I'd say there's no hard and fast rule. I'd probably check that the input we receive is sensible at the front-end and do the domain-level validation at the service layer.
我强烈建议使用 Hibernate Validator 并将所有验证保留到模型中。它对 Oops 封装有很大的促进作用,因为模型现在可以验证自身。
I would highly recommend using Hibernate Validator and keeping all your validations to the model. Its a good boost to Oops encapsulation as the model now validates itself.