码迷,mamicode.com
首页 > 编程语言 > 详细

spring mvc:练习:表单验证(javaConfig配置和注解)

时间:2018-05-24 11:56:13      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:结构   log   需要   att   enable   temp   add   .class   搜索   

使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS,JavaScript,图片)国际化支持我们的视图,使用ResourceHandlerRegistry,全部采用基于注解的配置。

请注意,JSR303是一种规范,hibernate-validator是我们在这篇文章中使用的一种实现,它也提供了几个不包含在规范中自己的验证注释。

  

插件:

validation-api 1.1.0.Final
hibernate-validator 5.1.2.Final

  

更新pom.xml,包括所需的依赖关系

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>gugua4</groupId>
  <artifactId>gugau6</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>gugau6 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  
    <properties>
  	<springVersion>4.3.5.RELEASE</springVersion>
  	<hibernate.validator>5.1.2.Final</hibernate.validator> <!-- hibernate.validator -->
  	<javax.validation>1.1.0.Final</javax.validation> <!--  JSR303 的验证注解 -->
  	<mysqlVersion>5.0.11</mysqlVersion>
  	<commonsDbcpVersion>1.4</commonsDbcpVersion>
  	<aspectjweaverVersion>1.8.13</aspectjweaverVersion>
  	<commonsLoggingVersion>1.2</commonsLoggingVersion>
  </properties>
  
  <dependencies>
  
     <!-- spring-test支持 -->
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-test</artifactId>
    	<version>${springVersion}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    
    
    <!-- spring模块库 -->
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-beans</artifactId>
    	<version>${springVersion}</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>${springVersion}</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-web</artifactId>
    	<version>${springVersion}</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>${springVersion}</version>
    </dependency>    
    <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-expression</artifactId>
	    <version>${springVersion}</version>
	</dependency>
	
	
	<!-- (aop)@Aspect注解及代理 -->
	<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
	<dependency>
	    <groupId>org.aspectj</groupId>
	    <artifactId>aspectjweaver</artifactId>
	    <version>1.6.8</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/cglib/cglib-nodep -->
	<dependency>
	    <groupId>cglib</groupId>
	    <artifactId>cglib-nodep</artifactId>
	    <version>2.1_3</version>
	</dependency>
    
    
    <!-- 依赖的持久化类库 -->
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.8</version>
    </dependency> 
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-tx</artifactId>
	    <version>3.0.5.RELEASE</version>
	</dependency>
	
    
    <!-- io流/上传类插件 -->
	<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
	<dependency>
	    <groupId>commons-io</groupId>
	    <artifactId>commons-io</artifactId>
	    <version>2.5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
	<dependency>
	    <groupId>commons-fileupload</groupId>
	    <artifactId>commons-fileupload</artifactId>
	    <version>1.3.2</version>
	</dependency>
    
    
     <!-- 连接池 -->
    <dependency>
    	<groupId>commons-dbcp</groupId>
    	<artifactId>commons-dbcp</artifactId>
    	<version>${commonsDbcpVersion}</version>
    </dependency>
    
    <!-- 公共基础类(字符处理,数组,日期,范围) -->   
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-lang3</artifactId>
	    <version>3.0</version>
	</dependency>
	
	
	 <!-- jsp依赖的web模块库 -->	    
	 <dependency>
	   <groupId>javax.servlet</groupId>
	   <artifactId>jsp-api</artifactId>
	   <version>2.0</version>
	   <scope>provided</scope>
	</dependency>
	<dependency>  
         <groupId>javax.servlet</groupId>  
         <artifactId>jstl</artifactId>  
         <version>1.2</version>  
         <scope>runtime</scope>  
    </dependency>  
    <dependency>
         <groupId>taglibs</groupId>
         <artifactId>standard</artifactId>
         <version>1.1.2</version>
     </dependency>
    
    <!-- servlet(HttpServletRequest,HttpServletResponse) -->	    
    <dependency>
	    <groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>3.1.0</version>
	    <!--  
	    <artifactId>servlet-api</artifactId>
	    <version>2.5</version>
	    <scope>provided</scope>
	    -->	    
	</dependency>
	
	
	<!-- jsr303 validation dependencies/ JSR303 的验证注解-->
		<dependency>
			<groupId>javax.validation</groupId>
			<artifactId>validation-api</artifactId>
			<version>${javax.validation}</version>
		</dependency>
		<!-- hibernate validation 数据验证-->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>${hibernate.validator}</version>
		</dependency>
    
    
  </dependencies>
  
  
  <build>
    <pluginManagement>
    	<plugins>
    		<plugin>
    			<groupId>org.apache.maven.plugins</groupId>
    			<artifactId>maven-war-plugin</artifactId>
    			<version>2.6</version>
    			<configuration>
    					<!-- jsp目录/项目访问目录 -->
    				<warSourceDirectory>src/main/webapp</warSourceDirectory>
    				<!-- 项目名 -->
    				<warName>gugau6</warName>
    				<!-- 取消web.xml配置 -->
    				<failOnMissingWebXml>false</failOnMissingWebXml>
    			</configuration>
    		</plugin>
    	</plugins>
    </pluginManagement> 
    <finalName>gugau6</finalName>
  </build>
  
  
</project>

  

首先要注意这里是 maven-war-plugin 插件声明。由于我们使用的是全注解配置,我们甚至不包括在 web.xml 中,所以我们需要配置这个插件,以避免Maven构建war包失败。在验证部分 validation-api 代表规范, 而hibernate-validator是本规范的一个实现。hibernate-validator还提供了一些它自己的注解(@Email,@NotEmpty等)不属于规范的一部分。

 

创建POJO/域对象

Student学生信息类

package springmvc.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Email;

import org.springframework.format.annotation.DateTimeFormat;


public class Student implements Serializable {

	@Size(min=3, max=30)
	private String firstName;
	
	@Size(min=3, max=30)
	private String lastName;
	
	@NotEmpty
	private String sex;
	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Past @NotNull
	private Date dob;
	
	@Email @NotEmpty
	private String email;
	
	@NotEmpty
	private String section;
	
	@NotEmpty
	private String country;
	
	
	private boolean firstAttempt;
	
	@NotEmpty
	private List<String> subjects = new ArrayList<String>();

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getDob() {
		return dob;
	}

	public void setDob(Date dob) {
		this.dob = dob;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSection() {
		return section;
	}

	public void setSection(String section) {
		this.section = section;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public boolean isFirstAttempt() {
		return firstAttempt;
	}

	public void setFirstAttempt(boolean firstAttempt) {
		this.firstAttempt = firstAttempt;
	}

	public List<String> getSubjects() {
		return subjects;
	}

	public void setSubjects(List<String> subjects) {
		this.subjects = subjects;
	}

	@Override
	public String toString() {
		return "Student [firstName=" + firstName + ", lastName=" + lastName + ", sex=" + sex + ", dob=" + dob
				+ ", email=" + email + ", section=" + section + ", country=" + country + ", firstAttempt=" + firstAttempt
				+ ", subjects=" + subjects + "]";
	}	
	
	
	
	
}

  

 

添加控制器

package springmvc.controller;

import java.util.ArrayList;
import java.util.List;

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 springmvc.model.Student;

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

@Controller
@RequestMapping(value="/")
public class HelloWorldController {

	
	@RequestMapping(method=RequestMethod.GET)
	public String newRegistration(ModelMap model)
	{
		Student student = new Student();
		model.addAttribute("student", student);
		return "hello_index";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String saveRegistration(@Valid Student student, BindingResult result, ModelMap model)
	{
		if(result.hasErrors())
		{
			//System.out.println(student.toString());
			
			//model.addAttribute("success", "Dear "+ student.getFirstName() + ", your Registration completed fail!");
			//return "hello_result";
			return "hello_index";
		}
		model.addAttribute("success", "Dear "+ student.getFirstName() + ", your Registration completed successfully!");
		return "hello_result";
		
	}
	
	@ModelAttribute("sections")
	public List<String> initializeSections()
	{
		List<String> sections = new ArrayList<String>();
		sections.add("graduate");
		sections.add("post graduate");
		sections.add("research");
		return sections;
	}
	
	@ModelAttribute("countries")
	public List<String> initializeCountries()
	{
		List<String> countries = new ArrayList<String>();
		countries.add("china");
		countries.add("usa");
		countries.add("germany");
		return countries;
	}
	
	@ModelAttribute("subjects")
	public List<String> initializeSubjects()
	{
		List<String> subjects = new ArrayList<String>();
		subjects.add("Physics");
		subjects.add("Chemistry");
		subjects.add("Life Science");
		return subjects;
	}
	
}

  

@Valid要求spring来验证相关的对象(学生)。 BindingResult包含此验证,并可能在此验证过程中发生(产生)任何错误的结果。请注意,BindingResult一定要在之后立即生效对象,否则spring将无法验证并且将一个异常抛出。

 

添加配置类

package springmvc.configuration;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;




@Configuration
@EnableWebMvc
@ComponentScan(basePackages="springmvc")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter {

	@Bean
	public ViewResolver viewResolver()
	{
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setViewClass(JstlView.class);
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub
		//super.addResourceHandlers(registry);
		//复写静态资源
		registry.addResourceHandler("/static/**").addResourceLocations("/static/");
	}

	
	public  MessageSource messageSource()
	{
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasename("messages");
		return messageSource;
	}
	
}

  

@Configuration指示该类包含注解为@Bean生产Bean管理是由Spring容器的一个或多个 bean 的方法。@EnableWebMvc 等效于 mvc:annotation-driven 在XML文件中。它能够为使用@RequestMapping 向特定的方法传入的请求映射@Controller-annotated类。 @ComponentScan 等效于  context:component-scan base-package="..." 提供具有到哪里查找管理Spring beans/类。

方法 ViewResolver 配置一个 ViewResolver 用来找出真正的视图。方法 addResourceHandlers 配置 ResourceHandler 静态资源。CSS, JavaScript, images 等都是静态的资源在你的页面里。上面的配置表示,所有的资源请求开始/static/,将从webapps文件夹下提供/static/。在这个例子中,我们把所有的css文件放在 Web应用程序的 /static/css 目录中。注意,此方法在 WebMvcConfigurerAdapter 中定义,因此我们需要扩展这个类来注册我们的静态资源覆盖此方法。

方法为 messageSource 配置消息包,以支持[国际化]消息属性文件。请注意方法 baseName 提供的参数(消息)。Spring 将搜索应用程序类路径中一个名为messages.properties文件。让我们添加的文件:

src/main/resources/messages.properties

Size.student.firstName=First Name must be between {2} and {1} characters long
Size.student.lastName=Last Name must be between {2} and {1} characters long
NotEmpty.student.sex=Please specify your gender
NotNull.student.dob=Date of birth can not be blank
Past.student.dob=Date of birth must be in the past
Email.student.email=Please provide a valid Email address
NotEmpty.student.email=Email can not be blank
NotEmpty.student.country=Please select your country
NotEmpty.student.section=Please select your section
NotEmpty.student.subjects=Please select at least one subject
typeMismatch=Invalid format

  

请注意,上述消息按照特定的模式

{ValidationAnnotationClass}.{modelObject}.{fieldName} 

  

此外,根据具体的注释(如@Size),也可以用传递参数给这些消息:{0},{1},..{i}

以XML格式上述结构将是
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
    <context:component-scan base-package="com.yiibai.springmvc" />
    <mvc:annotation-driven/>

    <mvc:resources mapping="/static/**" location="/static/" />
    <mvc:default-servlet-handler />


    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>messages</value>
        </property>
    </bean>


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
 
</beans>

  

添加视图(简单的JSP页面)

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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 enrollment</title>
<link href="<c:url value=‘/static/css/bootstrap.css‘ />"  ref="stylesheet">
<link href="<c:url value=‘/static/css/custom.css‘ />" ref="stylesheet">
</head>
<body>

<div class="form-container">
<h1>Enrollment Form</h1>
<form:form method="post" modelAttribute="student"  class="form-horizontal">
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="firstName">First Name</label>
			<div class="col-md-7">
				<form:input type="text" path="firstName" id="firstName" class="form-control input-sm" />
				<div class="has-error">
					<form:errors path="firstName" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="lastName">last Name</label>
			<div class="col-md-7">
				<form:input type="text" path="lastName" id="lastName" class="form-control input-sm" />
				<div class="has-error">
					<form:errors path="lastName" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="sex">sex</label>
			<div class="col-md-7">
				<form:radiobutton path="sex"  value="M" />Male
				<form:radiobutton path="sex" value="F" />Female
				<div class="has-error">
					<form:errors path="sex" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="email">email</label>
			<div class="col-md-7">
				<form:input path="email" id="email" class="form-control input-sm"/>
				<div class="has-error">
					<form:errors path="email" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="dob">dob</label>
			<div class="col-md-7">
				<form:input path="dob" id="dob" class="form-control input-sm"/>
				<div class="has-error">
					<form:errors path="dob" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="section">section</label>
			<div class="col-md-7">
				<form:radiobuttons path="section" id="section" items="${sections}"/>
				<div class="has-error">
					<form:errors path="section" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="country">country</label>
			<div class="col-md-7">
				<form:select path="country" id="country" class="form-control input-sm">
					<form:option value="">please select</form:option>
					<form:options items="${countries}"/>
				</form:select>
				<div class="has-error">
					<form:errors path="country" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	
	<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="firstAttempt">First Attempt ?</label>
				<div class="col-md-1">
					<form:checkbox path="firstAttempt" class="form-control input-sm"/>
					<div class="has-error">
						<form:errors path="firstAttempt" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>
	
	
	<div class="row">
		<div class="form-group col-md-12">
			<label class="col-md-3 control-label" for="subjects">subject</label>
			<div class="col-md-7">				
				<form:select path="subjects" items="${subjects}" multiple="true" class="form-control input-sm"/>
				<div class="has-error">
					<form:errors path="subjects" class="help-inline"/>
				</div>
			</div>
		</div>
	</div>
	
	<div class="row">
			<div class="form-actions floatRight">
				<input type="submit" value="Register" class="btn btn-primary btn-sm">
			</div>
		</div>

</form:form>
</div>


</body>
</html>

  

result.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>reuslt</title>
<link href="<c:url value=‘/static/css/custom.css‘/>" ref="stylesheet">
</head>
<body>

<div class="success">
		Confirmation message : ${success}
		<br>
		We have also sent you a confirmation mail to your email address : ${student.email}.
	</div>

</body>
</html>

  

 

添加初始化器类

package springmvc.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		// TODO Auto-generated method stub
		return new Class[] { HelloWorldConfiguration.class };
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String[] getServletMappings() {
		// TODO Auto-generated method stub
		return new String[] { "/"};
	}

}

  

有一点要记住,如:WebApplicationInitializer,Spring 基于Java 配置API是依赖Servlet3.0容器的。所以一定要确保你没有使用 Servlet 声明任何在 web.xml 小于3.0。对于我们的情况,我们要从应用程序中删除 web.xml 文件。

 

spring mvc:练习:表单验证(javaConfig配置和注解)

标签:结构   log   需要   att   enable   temp   add   .class   搜索   

原文地址:https://www.cnblogs.com/achengmu/p/9081574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!