zoukankan      html  css  js  c++  java
  • 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>
    

      

  • 相关阅读:
    【BZOJ 4581】【Usaco2016 Open】Field Reduction
    【BZOJ 4582】【Usaco2016 Open】Diamond Collector
    【BZOJ 4580】【Usaco2016 Open】248
    【BZOJ 3754】Tree之最小方差树
    【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版
    【51Nod 1622】【算法马拉松 19C】集合对
    【51Nod 1616】【算法马拉松 19B】最小集合
    【51Nod 1674】【算法马拉松 19A】区间的价值 V2
    【BZOJ 2541】【Vijos 1366】【CTSC 2000】冰原探险
    【BZOJ 1065】【Vijos 1826】【NOI 2008】奥运物流
  • 原文地址:https://www.cnblogs.com/achengmu/p/9036424.html
Copyright © 2011-2022 走看看