spring mvc: Hibernate验证器(字段不能为空,在1-150自己)
spring mvc: Hibernate验证器(字段不能为空,在1-150自己)
准备:
下载Hibernate Validator库 - Hibernate Validator。解压缩hibernate-validator-5.3.4.Final.jar
在/WEB-INF/下新建classes文件夹
访问地址:
http://localhost:8080/gugua3/student/index
项目:gugua3
包名: springtest
这里用到了hibernate-validator包中的,Range范围随机数注解,NotEmpty不为空注解
例如:
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range; public class Student { @NotEmpty
private String name; @Range(min=10, max=99)
private Integer age; private Integer id; }
配置文件:web.xml,applicationContext,springtest-servlet.xml

web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name> <!--配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 监听转发 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springtest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springtest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
springtest-servlet.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basename" value="messages"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
applicationContext.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认:注解映射支持 -->
<mvc:annotation-driven/>
<!-- 静态资源配置 -->
<mvc:resources location="/pages/**" mapping="/pages/"/> <!-- 自动扫描包名,controller -->
<context:component-scan base-package="springtest"/> </beans>
Student.java
package springtest; import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range; public class Student { @NotEmpty
private String name; @Range(min=10, max=99)
private Integer age; private Integer id; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} }
StudentController.java
package springtest; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import org.springframework.validation.annotation.Validated;
import org.springframework.validation.BindingResult; @Controller
public class StudentController { @RequestMapping(value="/student/index", method=RequestMethod.GET)
public ModelAndView student()
{
return new ModelAndView("student_index2", "command", new Student());
} @ModelAttribute("student")
public Student createStudent()
{
return new Student();
} @RequestMapping(value="/student/addStudent", method=RequestMethod.POST)
public String addStudent(@ModelAttribute("student") @Validated Student student,
BindingResult result, Model model)
{
if(result.hasErrors())
{
return "student_index2";
}
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "student_result2";
} }
student_index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>student index</title>
</head>
<style>
.error {
color: #ff0000;
} .errorblock {
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
<body> <h2>学生信息</h2>
<form:form method="post" action="/gugua3/student/addStudent" commandName="student">
<form:errors path="*" cssClass="errorblock" element="div" />
<table>
<tr>
<td><form:label path="name">姓名</form:label></td>
<td><form:input path="name"/></td>
<td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="age">年龄</form:label></td>
<td><form:input path="age"/></td>
<td><form:errors path="age" cssClass="error"/></td>
</tr>
<tr>
<td><form:label path="id">编号</form:label></td>
<td colspan="2"><form:input path="id"/></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" vlaue="提交"/>
</td>
</tr>
</table>
</form:form> </body>
</html>
student_result.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>student result</title>
</head>
<body> <table>
<tr>
<td>姓名</td>
<td>${name}</td>
</tr>
<tr>
<td>年龄</td>
<td>${age}</td>
</tr>
<tr>
<td>编号</td>
<td>${id}</td>
</tr>
</table> </body>
</html>
spring mvc: Hibernate验证器(字段不能为空,在1-150自己)的更多相关文章
- Spring MVC Hibernate验证器
下面的示例演示如何使用Spring Web MVC框架在表单中使用错误处理和验证器. 首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web ...
- Hibernate Validation,Spring mvc 数据验证框架注解
1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...
- Spring MVC第一课:用IDEA构建一个基于Spring MVC, Hibernate, My SQL的Maven项目
作为一个Spring MVC新手最基本的功夫就是学会如何使用开发工具创建一个完整的Spring MVC项目,本文站在一个新手的角度讲述如何一步一步创建一个基于Spring MVC, Hibernate ...
- Java Web 学习(6) —— Spring MVC 之校验器
Spring MVC 之校验器 数据验证 一个典型的 Spring MVC 应用会同时应用到 formatters/converters 和 validators. 在调用 controller 期间 ...
- Java 本地开发环境搭建(框架采用 Spring+Spring MVC+Hibernate+Jsp+Gradle+tomcat+mysql5.6)
项目搭建采用技术栈为:Spring+Spring MVC+Hibernate+Jsp+Gradle+tomcat+mysql5.6 搭建环境文档目录结构说明: 使用Intellj Idea 搭建项目过 ...
- spring mvc +cookie+拦截器功能 实现系统自动登陆
先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...
- Spring + Spring MVC + Hibernate
Spring + Spring MVC + Hibernate项目开发集成(注解) Posted on 2015-05-09 11:58 沐浴未来的我和你 阅读(307) 评论(0) 编辑 收藏 在自 ...
- Spring + Spring MVC + Hibernate项目开发集成(注解)
在自己从事的项目中都是使用xml配置的方式来进行的,随着项目的越来越大,会发现配置文件会相当的庞大,这个不利于项目的进行和后期的维护.于是考虑使用注解的方式来进行项目的开发,前些日子就抽空学习了一下. ...
- Hibernate验证器
第 4 章 Hibernate验证器 http://hibernate.org/validator/documentation/getting-started/#applying-constrain ...
随机推荐
- ASP.NET的优点
ASP.NET 是一个统一的 Web 开发平台,它提供开发人员创建企业级 Web 应用程序所需的服务.尽管 ASP.NET 的语法基本上与 ASP 兼容,但是它还提供了一个新的编程模型和基础结构以提高 ...
- Mac电脑下-nodejs安装卸载升级
一.Mac 安装nodejs: 1:brew install node 2:官网上下载指定版本(.pkg)双击安装 二.Mac 卸载nodejs: 1: brew的安装方式的卸载: brew un ...
- 推荐系统第6周--- SVD和基于标签的推荐系统
“隐语义”的真正背景 LSA(latent semantic analysis)潜在语义分析,也被称为LSI(latent semantic index),是Scott Deerweste ...
- C++学习笔记-操作符重载
操作符重载(operator overloading)是一种形式的C++多态,C++将操作符重载扩展到用户自定义的类型,如允许使用+将两个自定义的对象相加,编译器将根据操作数的数目和类型决定使用那种加 ...
- Mock Server 之 moco-runner 使用指南一
文章出处http://ju.outofmemory.cn/entry/96866 用以下命令可以启动moco-runner 服务 java -jar moco-runner-<version&g ...
- PHP计算上一个月最后一天、当月最后一天、下一个月最后一天
上个月最后一天: $last_month_last_day = date('Y-m-t',strtotime('-1 month')); 当月最后一天: $first_day=date('Y-m-01 ...
- centos7 vim显示行号
CentOS7下可能有n个账户,让vim显示行号有两种方法:仅让当前用户显示行号和让所有用户显示行号 一.仅让当前用户显示行号 输入命令:vim ~/.vimrc 写入:set nu 保存:wq ...
- idea 取消控制台的行数限制
有时候我们要输出大量的信息放到控制台显示,但是多了之后就出现最上面的信息被覆盖删除, 因此就需要设置控制台的显示行数,但在idea7之后的版本中,取消了对控制台行数设置选项, 只能通过更改配置文件进行 ...
- vue生命周期探究(二)
vue生命周期探究(二) 转载自:https://segmentfault.com/a/1190000008923105 上一章我们介绍了vue的组件生命周期和路由勾子,这一章,让我们来看看在vue- ...
- JVM(3) 垃圾回收器与内存分配策略
文章内容摘自:深入理解java虚拟机 第三章 对象已死? 1. 引用计数算法: 给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0 ...