javascript
spring mvc hibernate验证器使用示例 -凯发ag旗舰厅登录网址下载
下面的示例演示如何使用spring web mvc框架在表单中使用错误处理和验证器。 首先使用eclipse ide,并按照以下步骤使用spring web framework开发基于动态表单的web应用程序:
完整的项目文件目录结构如下所示 -
student.java 的代码如下所示 -
package com.yiibai.springmvc; import org.hibernate.validator.constraints.notempty; import org.hibernate.validator.constraints.range;public class student {@range(min = 1, max = 150) private integer age;@notemptyprivate string name;private integer id;public void setage(integer age) {this.age = age;}public integer getage() {return age;}public void setname(string name) {this.name = name;}public string getname() {return name;}public void setid(integer id) {this.id = id;}public integer getid() {return id;} }studentcontroller.java 的代码如下所示 -
package com.yiibai.springmvc; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.validation.bindingresult; import org.springframework.validation.annotation.validated; 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;@controller public class studentcontroller {@requestmapping(value = "/addstudent", method = requestmethod.get)public modelandview student() {return new modelandview("addstudent", "command", new student());}@modelattribute("student")public student createstudentmodel() { return new student();}@requestmapping(value = "/addstudent", method = requestmethod.post)public string addstudent(@modelattribute("student") @validated student student, bindingresult bindingresult, model model) {if (bindingresult.haserrors()) {return "addstudent";}model.addattribute("name", student.getname());model.addattribute("age", student.getage());model.addattribute("id", student.getid());return "result";} }message.properties 配置如下所示 -
notempty.student.name = name is required! range.student.age = age value must be between 1 and 150!这里的键可以是
hibernatevalidator-servlet.xml 配置如下所示 -
这里的第一个服务方法student(),已经在modelandview对象中传递了一个名称为“command”的空对象,因为如果在jsp文件中使用
第二个服务方法addstudent()将在url: hibernatevalidator/addstudent 上的post方法被调用。将根据提交的信息准备模型对象。 最后从服务方法返回“result”视图,这将渲染result.jsp。 如果使用validator生成错误,则返回相同的视图“addstudent”,则spring自动从视图中的bindingresult注入错误消息并显示出来。
addstudent.jsp 的代码如下所示 -
<%@ page contenttype="text/html; charset=utf-8" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>学生信息
上面的代码中使用了
它将呈现所有输入验证的错误消息。
使用带有path =“name”的
它将呈现姓名(name)和年龄(age)字段验证的错误消息。
result.jsp 的代码如下所示 -
<%@ page contenttype="text/html; charset=utf-8" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>提交的学生信息如下 -
姓名: | ${name} |
年龄: | ${age} |
编号: | ${id} |
完成创建源和配置文件后,发布应用程序到tomcat服务器。
现在启动tomcat服务器,当访问url => http://localhost:8080/hibernatevalidator/addstudent , 如果spring web应用程序没有问题,应该看到以下结果:
原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/spring_mvc/springmvc_hibernate_validator.html
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的spring mvc hibernate验证器使用示例的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: spring mvc生成pdf文件代码示