简体   繁体   English

Spring MVC如何进行表单验证(使用休眠)

[英]How does spring MVC do form validation (using hibernate)

I am following a tutorial, that showed us how to build using Spring MVC framework a simple form that can be validated. 我正在关注一个教程,该教程向我们展示了如何使用Spring MVC框架构建可以验证的简单表单。

We created a function annotated using RequestMapping then added @Valid as annotation to the first argument of this function along side @ModelAttribute annotation. 我们创建了一个使用RequestMapping进行注释的函数,然后在该函数的第一个参数的@ModelAttribute注释旁边添加了@Valid作为注释。 Let's call the first argument type ClassA . 我们将第一个参数类型称为ClassA

Now in ClassA we annotated some of its properties with hibernate validation annotations (eg @NotNull , @Pattern , etc...) 现在,在ClassA ,我们标注的一些属性与hibernate验证注释(例如@NotNull@Pattern ,等...)

What I am trying to understand is how is this all working? 我想了解的是这一切如何运作?

I checked the code for @interface NotNull for example and it really does nothing. 例如,我检查了@interface NotNull的代码,但实际上没有任何作用。

I am assuming that the spring MVC framework, uses reflection to detect that the first argument of the RequestMapping function is annotated using Valid , then when the form is submitted is calls SOMETHING to validate ClassA properties. 我假设spring MVC框架使用反射来检测使用Valid来注释RequestMapping函数的第一个参数,然后在提交表单时调用SOMETHING来验证ClassA属性。

Where is this SOMETHING ? SOMETHING在哪里? Is it in hibernate? 它处于休眠状态吗? Code please... How did Spring figure out to call hibernate code, we just added the hibernate jars to the project but at no point we told Spring to use them. 请编写代码... Spring如何找出要调用hibernate代码的,我们只是将hibernate jars添加到了项目中,但是我们丝毫没有告诉Spring使用它们。

Also, what is the specification that binds Spring MVC to hibernate? 另外,将Spring MVC绑定到休眠的规范是什么?

Can someone is simple terms and using high level (and references of code if possible) explain to me how things are working? 有人能用简单的术语并使用高级的代码(如果可能的话使用代码引用)向我解释事情的运行方式吗?

I am very new to this and coming from C++ where things are done very differently. 我对此很陌生,来自C ++,那里的工作方式截然不同。 I am trying to understand the mechanisms behind the workings of Spring . 我试图了解Spring运作背后的机制。

What you are seeing is Spring's support to JSR-303 Bean Validation API . 您将看到Spring对JSR-303 Bean验证API的支持。 Those annotations such as @NotNull , @Pattern are all part of this JSR. 这些注释,比如@NotNull@Pattern此JSR的一部分。 Since this is just a JSR (basically, JSR is just a set of interfaces), it needs a concrete implementation in order to actually work (called the provider). 由于这只是一个JSR(基本上,JSR只是一组接口),因此它需要一个具体的实现才能真正起作用(称为提供者)。 Here is where hibernate comes in the picture. 这是图片中出现休眠的地方。 Hibernate owns an implementation of JSR-303, called the hibernate-validator . Hibernate拥有JSR-303的实现,称为hibernate-validator This API has nothing to do with the core ORM functionalities of Hibernate. 该API与Hibernate的核心ORM功能无关。 In fact, this API is the reference implementation of JSR-303, that is why in the documentation you will see that it is used as the default provider. 实际上,此API是JSR-303的参考实现 ,这就是为什么在文档中您会看到它被用作默认提供程序的原因。

So as the user of Spring framework, all you need to know is Spring needs to find an implementation of JSR-303 in the classpath so that you can start using those annotation from javax.validation.* package. 因此,作为Spring框架的用户,您需要知道的是Spring需要在类路径中找到JSR-303的实现,以便您可以开始使用javax.validation.*包中的那些注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM