前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 4 MVC 表单校验资源处理(带源码)

Spring 4 MVC 表单校验资源处理(带源码)

作者头像
明明如月学长
发布2021-08-27 15:25:43
4520
发布2021-08-27 15:25:43
举报

【本系列其他教程正在陆续翻译中,点击分类:spring 4 mvc 进行查看】

【翻译 by 明明如月 QQ 605283073】

上一篇:Spring 4 MVC HelloWorld 纯注解方式(带源码)

下一篇文章:Spring 4 MVC 视图解析器(XML JSON PDF等) 纯注解

#项目下载地址:http://websystique.com/?smd_process_download=1&download_id=1258#

特别说明:此系列教程有的童鞋下载下来运行 经常404 或者改成xml方式以后

缺少org.springframework.web.context.ContextLoaderServlet

参见:http://blog.csdn.net/w605283073/article/details/52126347

本文我们将学习使用Spring 表单标签( Spring Form Tags),表单验证使用JSR-303验证注解( JSR-303 validation annotations)hibernate-validators,使用 MessageSource 实现国际化支持和ResourceHandlerRegistry实现访问静态资源 (e.g. CSS, javascript, images)  所有的这些都基于注解。

------------------------------------------

使用的技术或者软件

  • Spring 4.0.6.RELEASE
  • validation-api 1.1.0.Final
  • hibernate-validator 5.1.2.Final
  • Bootstrap v3.1.0
  • Maven 3
  • JDK 1.6
  • Tomcat 7.0.54
  • Eclipse JUNO Service Release 2

让我们开始吧。。

第1步: 创建项目目录结构

最终的项目结构

接下来 我们添加或者更新上面项目结构中的文件。

第2步: 修改pom.xml添加Spring 和Servlet 依赖
代码语言:javascript
复制
    4.0.0
    com.websystique.springmvc
    Spring4MVCFormValidationExample
    war
    1.0.0
    Spring4MVCFormValidationExample
 
    
        4.0.6.RELEASE
        5.1.2.Final
        1.1.0.Final
    
 
    
        
        
            org.springframework
            spring-core
            ${springframework.version}
        
        
            org.springframework
            spring-web
            ${springframework.version}
        
        
            org.springframework
            spring-webmvc
            ${springframework.version}
        
 
        
        
            javax.validation
            validation-api
            ${javax.validation.version}
        
        
            org.hibernate
            hibernate-validator
            ${hibernate.validator.version}
        
 
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.3.1
        
        
            javax.servlet
            jstl
            1.2
        
    
 
    
        
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.2
                    
                        1.6
                        1.6
                    
                
                
                    org.apache.maven.plugins
                    maven-war-plugin
                    2.4
                    
                        src/main/webapp
                        Spring4MVCFormValidationExample
                        false
                    
                
            
        
        Spring4MVCFormValidationExample

首先要注意的是 maven-war-plugin 的声明. 

因为我们想完

全不用xml,我们需要配置这个插件防止maven创建war包失败。

添加了JSP/Servlet/Jstl依赖,因为我们在视图代码里面将会用到servlet api和jstl表达式。

一般来说,容器已经包含了这些库,你可以在pom.xml文件中设置它们的scope为provided 。

第3步:创建 POJO / Domain 对象

此domain 对象将扮演承载表单中的user数据的角色。

想要验证的属性我们将加上注解。

com.websystique.springmvc.model.Student

代码语言:javascript
复制
package com.websystique.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.Email;
import org.hibernate.validator.constraints.NotEmpty;
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="dd/MM/yyyy")
    @Past @NotNull
    private Date dob;
 
    @Email @NotEmpty
    private String email;
 
    @NotEmpty
    private String section;
 
    @NotEmpty
    private String country;
 
    private boolean firstAttempt;
 
    @NotEmpty
    private List subjects = new ArrayList();
 
    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 getSubjects() {
        return subjects;
    }
 
    public void setSubjects(List 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
                + "]";
    }
 
}

上面代码中  @Size@Past  &  @NotNull  是标准验证注解  @NotEmpty  &  @Email  则不是(来自hibernate或者spring).

Step 4: 添加控制器

com.websystique.springmvc.controller.HelloWorldController

代码语言:javascript
复制
package com.websystique.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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.websystique.springmvc.model.Student;
 
@Controller
@RequestMapping("/")
public class HelloWorldController {
 
    /*
     * This method will serve as default GET handler.
     *
     */
    @RequestMapping(method = RequestMethod.GET)
    public String newRegistration(ModelMap model) {
        Student student = new Student();
        model.addAttribute("student", student);
        return "enroll";
    }
 
    /*
     * This method will be called on form submission, handling POST request
     * It also validates the user input
     */
    @RequestMapping(method = RequestMethod.POST)
    public String saveRegistration(@Valid Student student, BindingResult result, ModelMap model){
 
        if(result.hasErrors()) {
                return "enroll";
        }
 
        model.addAttribute("success", "Dear "+ student.getFirstName()+" , your Registration completed successfully");
        return "success";
    }
 
    /*
     * Method used to populate the Section list in view.
     * Note that here you can call external systems to provide real data.
     */
    @ModelAttribute("sections")
    public List initializeSections() {
 
        List sections = new ArrayList();
        sections.add("Graduate");
        sections.add("Post Graduate");
        sections.add("Research");
        return sections;
    }
 
    /*
     * Method used to populate the country list in view.
     * Note that here you can call external systems to provide real data.
     */
    @ModelAttribute("countries")
    public List initializeCountries() {
 
        List countries = new ArrayList();
        countries.add("USA");
        countries.add("CANADA");
        countries.add("FRANCE");
        countries.add("GERMANY");
        countries.add("ITALY");
        countries.add("OTHER");
        return countries;
    }
 
    /*
     * Method used to populate the subjects list in view.
     * Note that here you can call external systems to provide real data.
     */
    @ModelAttribute("subjects")
    public List initializeSubjects() {
 
        List subjects = new ArrayList();
        subjects.add("Physics");
        subjects.add("Chemistry");
        subjects.add("Life Science");
        subjects.add("Political Science");
        subjects.add("Computer Science");
        subjects.add("Mathmatics");
        return subjects;
    }
 
}

@Controller 注解表明此控制器将用来处理@RequestMapping映射的请求.带有‘/’,说明这个是默认的开工资器.  newRegistration 带有@RequestMethod.GET 处理默认的Get请求, 在模型中添加了student对象为表单提供数据,。

initializeSections, initializeCountries & initializeSubjects 等方法,简单的创建request级别的对象,他们的值将在view或者jsp页面里使用。

带有@RequestMethod.POST 注解的saveRegistration方法 将用来处理表单的POST提交.注意此方法参数的顺序@Valid 要求spring校验对象(student). BindingResult 包含校验过程中包含的错误等信息.注意 BindingResult必修跟在被检验对象的后面,否则spring不仅不进行对象的校验而且还会抛出异常。

注意:如果校验失败,我们不想要的效果:默认或者一般的错误信息将会输出在屏幕上。因此我们可以重写欣慰提供者internationalized messages 指明每个属性。要想这样做,我们需要配置应用中的MessageSource 配置文件提供实际消息的属性文件。

第5步: 添加配置类
代码语言:javascript
复制
com.websystique.springmvc.configuration.HelloWorldConfiguration
代码语言:javascript
复制
package com.websystique.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 = "com.websystique.springmvc")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter {
 
    /*
     * Configure View Resolver
     */
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
 
    /*
     * Configure ResourceHandlers to serve static resources like CSS/ Javascript etc...
     *
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }
 
    /*
     * Configure MessageSource to provide internationalized messages
     *
     */
 
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
 
}

@Configuration  表明此类包含一个或者多个含有 @Bean注解的方法,提供spring 容器的bean的管理。

@EnableWebMvc 等价于 xml中的mvc:annotation-driven  . 他使得 @Controller注解的类通过@RequestMapping 来映射请求url对应的处理器。

@ComponentScan等价于 context:component-scan base-package="..." 通过此配置spring来扫描bean

viewResolver 配置视图解析器解析到对应的视图.addResourceHandlers 方法为页面所需的静态资源如resources. CSS, JavaScript, images等添加ResourceHandler .上面的配置意思是说所有的对 /static/的资源请求都对应webapp文件夹下的 /static/ 文件夹. 

messageSource 配置一个 消息绑定绑定 [internationalized] 配置文件中的消息. 注意baseName 方法设置的值(messages). 因此Spring 将在类路径搜索messages.properties.

src/main/resources/messages.properties

代码语言:javascript
复制
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

注意:上面消息的模式

{验证注解类}.{模型对象}.{属性名}

另外, 对于特定的注解 (如@Size)你也可以使用 {0},{1},..{i}等传递这些消息的参数。

xml配置形式

代码语言:javascript
复制
            messages
        
    
 
 
    
        
            /WEB-INF/views/
        
        
            .jsp
第6步: 添加视图(简单得JSP页面)

WEB-INF/views/enroll.jsp

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 

 

    
    Student Enrollment Form
    
    

 

 
    
     
    Enrollment Form
     
    
 
        
            
                First Name
                
                    
                    
                        
                    
                
            
        
 
        
            
                Last Name
                
                    
                    
                        
                    
                
            
        
 
        
            
                Sex
                
                    Male 
                    Female
                    
                        
                    
                
            
        
 
        
            
                Date of birth
                
                    
                    
                        
                    
                
            
        
 
        
            
                Email
                
                    
                    
                        
                    
                
            
        
 
 
        
            
                Section
                
                    
                    
                        
                    
                
            
        
 
 
        
            
                Country
                
                    
                        Select Country
                        
                    
                    
                        
                    
                
            
        
 
        
            
                First Attempt ?
                
                    
                    
                        
                    
                
            
        
     
 
        
            
                Subjects

WEB-INF/views/success.jsp

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    
    Student Enrollment Detail Confirmation
    


    
        Confirmation message : ${success}
        

        We have also sent you a confirmation mail to your email address : ${student.email}.
第7步:添加初始化类
代码语言:javascript
复制
com.websystique.springmvc.configuration.HelloWorldInitializer
代码语言:javascript
复制
package com.websystique.springmvc.configuration;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
 
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
 
public class HelloWorldInitializer implements WebApplicationInitializer {
 
    public void onStartup(ServletContext container) throws ServletException {
 
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(HelloWorldConfiguration.class);
        ctx.setServletContext(container);
 
        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));
 
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
 
}

当然也可以通过继承 AbstractAnnotationConfigDispatcherServletInitializer类来简化上面的类

代码语言:javascript
复制
package com.websystique.springmvc.configuration;
 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 
public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[] { HelloWorldConfiguration.class };
    }
  
    @Override
    protected Class[] getServletConfigClasses() {
        return null;
    }
  
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
 
}
第8步: 创建和发布应用

如果现在直接提交,将会看到校验的错误信息 和message.properties对应

简单输入一些

再次提交

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/05/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第1步: 创建项目目录结构
  • 第2步: 修改pom.xml添加Spring 和Servlet 依赖
  • 第3步:创建 POJO / Domain 对象
  • Step 4: 添加控制器
  • 第5步: 添加配置类
  • 第6步: 添加视图(简单得JSP页面)
  • 第7步:添加初始化类
  • 第8步: 创建和发布应用
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档