日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 3 MVC and JSR303 @Valid example

發布時間:2025/3/17 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 3 MVC and JSR303 @Valid example 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

————————————————————————————————————————————————————————

用jsr303驗證表單數據,搞了很長時間,驗證倒是驗證了,但是@Valid不起作用,表現是:

無論是否加@Valid, 都會驗證,并且spring無法捕獲到異常BindingResult.hasErrors始終是false。

上面那個例子就可以。具體原因還不是很明了,按上面那個例子,對有問題的項目做如下處理:

1 使用<mvc:annotation-driven />

2 去掉DefaultAnnotationHandlerMapping, ControllerClassNameHandlerMapping, AnnotationMethodHandlerAdapter

3 因為去掉了ControllerClassNameHandlerMapping,在控制器做相應處理

————————————————————————————

現在想想之前嘗試各種辦法都不成功,有沒有可能跟這個異常有關系呢?應該沒有,之前應該已經嘗試過這種情況。

Caused by: java.lang.OutOfMemoryError: PermGen space

————————————————————————————————————————————————————————

In Spring 3, you can enable “mvc:annotation-driven” to support JSR303 bean validation via @Valid annotation, if any JSR 303 validator framework on the classpath.

Note
Hibernate Validator is the reference implementation for JSR 303

In this tutorial, we show you how to integrate Hibernate validator with Spring MVC, via @Valid annotation, to perform bean validation in a HTML form.

Technologies used :

  • Spring 3.0.5.RELEASE
  • Hibernate Validator 4.2.0.Final
  • JDK 1.6
  • Eclipse 3.6
  • Maven 3
  • 1. Project Dependencies

    Hibernate validator is available at JBoss public repository.

    <repositories><repository><id>JBoss repository</id><url>http://repository.jboss.org/nexus/content/groups/public/</url></repository></repositories><properties><spring.version>3.0.5.RELEASE</spring.version></properties><dependencies><!-- Spring 3 dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- Hibernate Validator --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>4.2.0.Final</version></dependency></dependencies>

    2. JSR303 Bean Validation

    A simple POJO, annotated with Hibernate validator annotation.

    Note
    Refer to this Hibernate validator documentation for detail explanation package com.mkyong.common.model;import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.Range;public class Customer {@NotEmpty //make sure name is not emptyString name;@Range(min = 1, max = 150) //age need between 1 and 150int age;//getter and setter methods}

    3. Controller + @Valid

    For validation to work, just annotate the “JSR annotated model object” via @Valid. That’s all, other stuff is just a normal Spring MVC form handling.

    package com.mkyong.common.controller;import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.mkyong.common.model.Customer;@Controller @RequestMapping("/customer") public class SignUpController {@RequestMapping(value = "/signup", method = RequestMethod.POST)public String addCustomer(@Valid Customer customer, BindingResult result) {if (result.hasErrors()) {return "SignUpForm";} else {return "Done";}}@RequestMapping(method = RequestMethod.GET)public String displayCustomerForm(ModelMap model) {model.addAttribute("customer", new Customer());return "SignUpForm";}}

    4. Error Message

    By default, if validation failed.

  • @NotEmpty will display “may not be empty”
  • @Range will display “must be between 1 and 150″
  • You can override it easily, create a properties with “key” and message. To know which @annotation bind to which key, just debug it and view value inside “BindingResult result“. Normally, the key is “@Annotation Name.object.fieldname“.

    File : messages.properties

    NotEmpty.customer.name = Name is required! Range.customer.age = Age value must be between 1 and 150

    5. mvc:annotation-driven

    Enable “mvc:annotation-driven” to make Spring MVC supports JSR303 validator via @Valid, and also bind your properties file.

    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.mkyong.common.controller" /><!-- support JSR303 annotation if JSR 303 validation present on classpath --><mvc:annotation-driven /><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix"><value>/WEB-INF/pages/</value></property><property name="suffix"><value>.jsp</value></property></bean><!-- bind your messages.properties --><bean class="org.springframework.context.support.ResourceBundleMessageSource"id="messageSource"><property name="basename" value="messages" /></bean></beans>

    6. JSP Pages

    Last one, normal JSP page with Spring form tag library.

    File : SignUpForm.jsp

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <style> .error {color: #ff0000; }.errorblock {color: #000;background-color: #ffEEEE;border: 3px solid #ff0000;padding: 8px;margin: 16px; } </style> </head><body><h2>Customer SignUp Form - JSR303 @Valid example</h2><form:form method="POST" commandName="customer" action="customer/signup"><form:errors path="*" cssClass="errorblock" element="div" /><table><tr><td>Customer Name :</td><td><form:input path="name" /></td><td><form:errors path="name" cssClass="error" /></td></tr><tr><td>Customer Age :</td><td><form:input path="age" /></td><td><form:errors path="age" cssClass="error" /></td></tr><tr><td colspan="3"><input type="submit" /></td></tr></table></form:form></body> </html>

    File : Done.jsp

    <html> <body><h2>Done</h2> </body> </html>

    6. Demo

    URL : http://localhost:8080/SpringMVC/customer – Customer form page, with 2 text boxes for name and age.

    URL : http://localhost:8080/SpringMVC/customer/signup – If you didn’t fill in the form and click on the “submit” button, your customized validation error messages will be displayed.

    Download Source Code

    Download it – SpringMVC-Bean-Validation-JSR303-Example-2.zip (9 KB)

    References

  • Spring JSR303 validation documentation
  • JSR303 bean validation
  • Hibernate Validator
  • ?

    ?

    總結

    以上是生活随笔為你收集整理的Spring 3 MVC and JSR303 @Valid example的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。