Spring 3 MVC And JSR303 @Valid Example


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

package com.xxx.training.controller;

 

import com.xxx.training.model.User;

import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

 

import javax.validation.Valid;

 

@Controller

@RequestMapping(value = "/user")

public class SignUpController {

    @RequestMapping(value = "signup", method = RequestMethod.POST)

    public ModelAndView signUp(@Valid User user, BindingResult result) {

        if (result.hasErrors()) {

            return new ModelAndView("signUpForm");

        }

        return new ModelAndView("done");

    }

 

    @RequestMapping(method = RequestMethod.GET)

    public ModelAndView displaySignUpForm() {

        return new ModelAndView("signUpForm""user"new User());

    }

}

  


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

package com.xxx.training.model;

 

 

import org.hibernate.validator.constraints.NotEmpty;

import org.hibernate.validator.constraints.Range;

 

public class User {

 

    @NotEmpty

    private String name;

 

    @Range(min = 1, max = 150)

    private int age;

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

}

  message.properties


1

2

NotEmpty.user.name = Name is required!

Range.user.age = Age value must be between 1 and 150

  signUpForm.jsp


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<%@ 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="user" action="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>

  dispatchServlet-servlet.xml


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

 

    <context:component-scan base-package="com.xxx.training"/>

 

    <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>

 

    <!-- support JSR303 annotation if JSR 303 validation present on classpath -->

    <mvc:annotation-driven />

 

    <bean class="org.springframework.context.support.ResourceBundleMessageSource"

          id="messageSource">

        <property name="basename" value="messages" />

    </bean>

 

</beans>

  web。xml:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

 

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

 

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.png</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.js</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.css</url-pattern>

    </servlet-mapping>

 

    <servlet>

        <servlet-name>dispatcherServlet</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>dispatcherServlet</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

 

    <filter>

        <filter-name>CharacterEncodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>

            <param-name>encoding</param-name>

            <param-value>UTF-8</param-value>

        </init-param>

    </filter>

    <filter-mapping>

        <filter-name>CharacterEncodingFilter</filter-name>

        <url-pattern>/</url-pattern>

    </filter-mapping>

 

    <error-page>

        <error-code>404</error-code>

        <location>/WEB-INF/pages/404.jsp</location>

    </error-page>

 

</web-app>

时间: 2024-09-17 13:58:49

Spring 3 MVC And JSR303 @Valid Example的相关文章

在Spring Web MVC环境下使用Dojo

开始之前 关于本教程 本教程主要探讨如何在 Spring Web MVC 环境中使用 Dojo 的 widget,示例应用使用了 dojox.data.DataGrid,一个 Dojo Toolkit 1.2 新增的 widget . Dojo widget 与服务器交换数据的格式有很多种,本教程主要探讨在 Ajax 编程中比较常用的 JSON 格式的数据.本教程示例演示了 dojox.data.DataGrid 组件与 Spring Web MVC 控制器之间交换数据的细节,其中,服务器端使用

Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

Spring Boot--2分钟构建spring web mvc REST风格HelloWorld 之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring boot如何快速构建一个.   Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以"即时运行"的应用和服务.支持约定大于配置,目的是尽可能快地构建和运行Spring应

Features of Spring Web MVC

21.1.1 Features of Spring Web MVC Spring Web Flow Spring Web Flow (SWF) aims to be the best solution for the management of web application page flow. SWF integrates with existing frameworks like Spring MVC and JSF, in both Servlet and Portlet environ

spring 的mvc中有没有像struts的DispatchAction

问题描述 还是不喜欢,一个请求写一个action那也太烦了最好用spring的mvc也可以用像DispatchAction一样的action昂昂用的是spring mvc 解决方案 Spring中有个MultiActionController解决方案二:public class HomeController extends MultiActionController {public List list;//查询所有数据public ModelAndView TestOne(HttpServlet

Spring 4 MVC+Apache Tiles 3 Example

In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuration. Apache Tiles is a template based, composite view framework: it allows to reuse page pieces across the application, keeping consistent look and feel

spring mvc使用jsr303验证的问题

问题描述 描述一下,就是如果接收的数据是多层的,也就是我ModelA类的属性ModelB也是一个对象,我传入的是ModelB中的属性,传入方式为ModelB.xxx,命令对象为ModelA,那么xxx属性上的验证将不起作用,不知道是我自己哪里没配置好还是spring mvc的验证不支持,求大神帮忙解答. 解决方案 A @Valid //加这个即可 B

spring在MVC层解决JPA的缓迟加载问题

作为EJB3.0的一部分,JPA是一个好东西.其简单的配置方式及强大的默认配置支持,使其可以轻松自由的存在于轻量与重量之间,如果现在您的JavaEE项目,不管是选择轻量级构架还是重量级构架,如果持久层不选择使用JPA,而是用一些ORM框架(如Hibernate.TopLink)的专用API,那么在将来的某一天一定会为这个选择而说出至尊宝那句"假如上天再给我一个机会-"的至理名言.下面是一个简单的Entity,是对一个CMS系统中,关于树状信息目录实体类的定义,包括了一些详细的映射的配置

spring mvc-spring MVC jsp页面获取参数 以对象的方式

问题描述 spring MVC jsp页面获取参数 以对象的方式 jsp里面的body部分 <h1>用户信息添加2</h1> <form action="user/add3.do" method="post"> 编号:<input type="text" name="userId"/><br/> 姓名:<input type="text" n

spring json-spring mvc 表单提交问题 请教

问题描述 spring mvc 表单提交问题 请教 请先参考附图.. 请教问题如下: 1.保存时,标识1 的基本信息 和 标识2 的订单明细信息一块提交保存么? 2.如果上述的标识1 和 标识2 的信息一块保存,怎么保存? 个人经历: spring 的controller 如果是如下的一般(非接收json)方式,如下: @RequestMapping(value = "create", method = RequestMethod.POST) @ResponseBody public