飞道的博客

SpringMVC二、SpringMVC使用基本使用

250人阅读  评论(0)

1、SpringMVC对请求参数的处理

在之前的servlet中我们可以通过request.getParameter()来获取请求中的参数,但是在我们编写的SpringMVC的应用程序中,在具体请求的方法中并不包含request参数,那么我们应该如何获取请求中的参数呢?

需要使用以下几个注解:

  • @RequestParam:获取请求的参数
  • @RequestHeader:获取请求头信息
  • @CookieValue:获取cookie中的值

1>、@RequestParam的基本使用

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RequestController {

    /**
     * 如何获取SpringMVC中请求中的信息
     *  默认情况下,可以直接在方法的参数中填写跟请求一样的名称,此时会默认接受参数
     *      如果有值,直接赋值,如果没有,那么直接给空值
     *
     * @RequestParam:获取请求中的参数值,使用此注解之后,参数的名称不需要跟请求的名称一致,但是必须要写
     *      public String request(@RequestParam("user") String username){
     *
     *      此注解还包含三个参数:
     *      value:表示要获取的参数值
     *      required:表示此参数是否必须,默认是true,如果不写参数那么会报错,如果值为false,那么不写参数不会有任何错误
     *      defaultValue:如果在使用的时候没有传递参数,那么定义默认值即可
     *
     *
     * @param username
     * @return
     */
    @RequestMapping("/request")
    public String request(@RequestParam(value = "user",required = false,defaultValue = "hehe") String username){
        System.out.println(username);
        return "success";
    }
}

2>、@RequestHeader的基本使用

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.management.resources.agent;

@Controller
public class RequestController {

    /**
     * 如果需要获取请求头信息该如何处理呢?
     *  可以使用@RequestHeader注解,
     *      public String header(@RequestHeader("User-Agent") String agent){
     *      相当于  request.getHeader("User-Agent")
     *
     *      如果要获取请求头中没有的信息,那么此时会报错,同样,此注解中也包含三个参数,跟@RequestParam一样
     *          value
     *          required
     *          defalutValue
     * @param agent
     * @return
     */
    @RequestMapping("/header")
    public String header(@RequestHeader("User-Agent") String agent){
        System.out.println(agent);
        return "success";
    }
}

3>、@CookieValue的基本使用

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.management.resources.agent;

@Controller
public class RequestController {

    /**
     * 如果需要获取cookie信息该如何处理呢?
     *  可以使用@CookieValue注解,
     *      public String cookie(@CookieValue("JSESSIONID") String id){
     *      相当于
     *      Cookie[] cookies = request.getCookies();
     *      for(Cookie cookie : cookies){
     *          cookie.getValue();
     *      }
     *      如果要获取cookie中没有的信息,那么此时会报错,同样,此注解中也包含三个参数,跟@RequestParam一样
     *          value
     *          required
     *          defalutValue
     * @param id
     * @return
     */
    @RequestMapping("/cookie")
    public String cookie(@CookieValue("JSESSIONID") String id){
        System.out.println(id);
        return "success";
    }
}

4>、请求中传递对象属性值

如果请求中传递的是某一个对象的各个属性值,此时如何在控制器的方法中获取对象的各个属性值呢?

​在SpringMVC的控制中,能直接完成对象的属性赋值操作,不需要人为干预。

Address.java

package com.bobo.entry;

public class Address {
    private String province;
    private String city;
    private String town;

    public Address() {
    }

    public Address(String province, String city, String town) {
        this.province = province;
        this.city = city;
        this.town = town;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", town='" + town + '\'' +
                '}';
    }
}

User.java

package com.bobo.entry;

public class User {
    private Integer id;
    private String name;
    private String age;
    private String gender;
    private Address address;

    public User() {
    }

    public User(Integer id, String name, String age, String gender, Address address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", gender='" + gender + '\'' +
                ", address=" + address +
                '}';
    }
}

RequestController.java

package com.bobo.controller;

import com.bobo.entry.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RequestController {
    @RequestMapping("/requestUser")
    public String testRequestParam(User user) {
        System.out.println(user);
        return "success";
    }
}

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: zhaosongbo
  Date: 2020/4/6
  Time: 5:27 下午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  pageContext.setAttribute("cpt",request.getContextPath());
%>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form action="${cpt}/requestUser" method="post">
    编号:<input type="text" name="id"/><br>
    姓名:<input type="text" name="name"/><br>
    年龄:<input type="text" name="age"/><br>
    性别:<input type="text" name="gender"/><br>
    省份:<input type="text" name="address.province"/><br>
    城市:<input type="text" name="address.city"/><br>
    县城:<input type="text" name="address.town"/><br>
    <input type="submit" value="提交"/><br>
  </form>
  </body>
</html>

2、乱码问题解决

我们在表单或者发送请求的时候,经常会遇到中文乱码的问题,那么如何解决乱码问题呢?

​ GET请求:在server.xml文件中,添加URIEncoding=“UTF-8”

​ POST请求:编写过滤器进行实现

注意:如果配置了多个过滤器,那么字符编码过滤器一定要在最前面,否则失效。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
       关联springmvc的配置文件:
       此配置文件的属性可以不添加,但是需要在WEB-INF的目录下创建 前端控制器名称-servlet.xml文件
       -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application_context.xml</param-value>
        </init-param>
    </servlet>
    <!--匹配servlet的请求,
    /:标识匹配所有请求,但是不会jsp页面
    /*:拦截所有请求,拦截jsp页面

     但是需要注意的是,当配置成index.html的时候,会发现请求不到
     原因在于,tomcat下也有一个web.xml文件,所有的项目下web.xml文件都需要继承此web.xml
     在服务器的web.xml文件中有一个DefaultServlet用来处理静态资源,但是url-pattern是/
     而我们在自己的配置文件中如果添加了url-pattern=/会覆盖父类中的url-pattern,此时在请求的时候
     DispatcherServlet会去controller中做匹配,找不到则直接报404
     而在服务器的web.xml文件中包含了一个JspServlet的处理,所以不过拦截jsp请求
    -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--解决post请求乱码-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!--解决响应乱码-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

3、SpringMVC对原生API的支持

package com.bobo.controller;

import com.bobo.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.PrintWriter;

@Controller
public class UserController {

    @RequestMapping("/addUser")
    public String addUser(User user){
        System.out.println(user);
        return "success";
    }

    /**
     * SpringMVC也可以在参数上使用原生的Servlet API
     *
     *  HttpSession
     *  HttpServletRequest
     *  HttpServletResponse
     *
     *  java.security.Principal 安全协议相关
     *  Locale:国际化相关的区域信息对象
     *  InputStream:
     *      ServletInputStream inputStream = request.getInputStream();
     *  OutputStream:
     *      ServletOutputStream outputStream = response.getOutputStream();
     *  Reader:
     *      BufferedReader reader = request.getReader();
     *  Writer:
     *      PrintWriter writer = response.getWriter();
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("api")
    public String api(HttpSession session, HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("requestParam","request");
        session.setAttribute("sessionParam","session");
        return "success";
    }
}

4、使用Model,Map,ModelMap传输数据到页面

在刚开始的helloworld项目中,我们传递了参数回到我们页面,但是后续的操作都只是接受用户的请求,那么在SpringMVC中除了可以使用原生servlet的对象传递数据之外,还有什么其他的方式呢?

可以在方法的参数上传入Model,ModelMap,Map类型,此时都能够将数据传送回页面:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <filter>
        <filter-name>encoding</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application_context.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

application_context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.bobo"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/page/"></property>
    </bean>
</beans>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
欢迎,${msg}
</body>
</html>

ResponseController.java

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
public class ResponseController {
    @RequestMapping("/outPut1")
    public String outPut1(Model model){
        System.out.println("outPut1");
        model.addAttribute("msg","我是model,outPut1");
        return "success";
    }
    @RequestMapping("/outPut2")
    public String outPut2(ModelMap modelMap){
        System.out.println("outPut2");
        modelMap.addAttribute("msg","我是ModelAndView,outPut2");
        return "success";
    }

    @RequestMapping("outPut3")
    public String outPut3(Map map){
        System.out.println("outPut3");
        map.put("msg","我是map,outPut3");
        return "success";
    }
}

请求结果:
http://localhost:8080/my_spring_mvc02_war_exploded/outPut1

当使用此方式进行设置之后,会发现所有的参数值都设置到了request作用域中,那么这三个对象是什么关系呢?

5、使用ModelAndView对象传输数据到页面

    @RequestMapping("/outPut4")
    public ModelAndView outPut4(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("msg","我是ModelAndView");
        return modelAndView;
    }

发现当使用modelAndView对象的时候,返回值的类型也是此对象,可以将要跳转的页面设置成view的名称,来完成跳转的功能,同时数据也是放到request作用中。

6、使用session传输数据到页面

@SessionAttributes:此注解可以表示,当向request作用域设置数据的时候同时也要向session中保存一份,此注解有两个参数,一个value(表示将哪些值设置到session中),另外一个type(表示按照类型来设置数据,一般不用,因为有可能会将很多数据都设置到session中,导致session异常)。

SessionAttributeController.java

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

@SessionAttributes("msg")
@Controller
public class SessionAttributeController {
    @RequestMapping("testSessionAttribute")
    public String testSessionAttribute(Model model){
        model.addAttribute("msg","我是sessionAttribute");
        return "success";
    }
}

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
page:${pageScope.msg}<br>
request:${requestScope.msg}<br>
session:${sessionScope.msg}<br>
application:${applicationScope.msg}<br>
</body>
</html>

运行结果:

7、使用@ModelAttribute来获取请求中的数据

@ModelAttribute注解用于将方法的参数或者方法的返回值绑定到指定的模型属性上,并返回给web视图。首先来介绍一个业务场景,来帮助大家做理解,在实际工作中,有些时候我们在修改数据的时候可能只需要修改其中几个字段,而不是全部的属性字段都获取,那么当提交属性的时候,从form表单中获取的数据就有可能只包含了部分属性,此时再向数据库更新的时候,肯定会丢失属性,因为对象的封装是springmvc自动帮我们new的,所以此时需要先将从数据库获取的对象保存下来,当提交的时候不是new新的对象,而是在原来的对象上进行属性覆盖,此时就需要使用@ModelAttribute注解。

User.java

package com.bobo.model;

public class User {
    private Integer id;
    private String name;
    private String password;
    private Integer age;

    public User() {
    }

    public User(Integer id, String name, String password, Integer age) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

ModelAttributeController.java

package com.bobo.controller;

import com.bobo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ModelAttributeController {

    Object o1 = null;
    Object o2 = null;
    Object o3 = null;

    @RequestMapping("/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("user") User user, Model model) {
        System.out.println("testModelAttribute" + user);
        o3 = model;
        //可以看到所有的model都是同一个对象
        System.out.println(o1 == o3);
        //可以看到存储的user对象也是同一个
        System.out.println(o2 == user);
        return "success";
    }

    @ModelAttribute
    public void MyModelAttribute(Model model) {
        o1 = model;
        User user = new User();
        user.setId(100);
        user.setName("bobo");
        user.setPassword("123");
        user.setAge(12);
        model.addAttribute("user", user);
        o2 = user;
        System.out.println("MyModelAttribute:" + user);
    }
}


index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
    <%request.setAttribute("ctp",request.getContextPath());%>
  </head>
  <body>
  <form action="${ctp}/testModelAttribute">
    id:<input type="text" name="id"/><br>
    name:张三<br>
    password:<input type="text" name="password"/><br>
    age:<input type="text" name="age"/><br>
    <input type="submit" value="提交"/>
  </form>
  </body>
</html>

输入值:

提交后打印:

MyModelAttribute:User{id=100, name='bobo', password='123', age=12}
testModelAttributeUser{id=1, name='bobo', password='1', age=1}
true
true

其实在使用的时候可以简化写法,也就是说,在方法的参数上不加@ModelAttribute也不会有问题

	@RequestMapping("/testModelAttribute")
    public String testModelAttribute(User user, Model model) {
        System.out.println("testModelAttribute" + user);
        o3 = model;
        //可以看到所有的model都是同一个对象
        System.out.println(o1 == o3);
        //可以看到存储的user对象也是同一个
        System.out.println(o2 == user);
        return "success";
    }

如果添加的@ModelAttribute(“”)属性的值不对,那么也是获取不到值的。同时可以添加@SessionAttributes属性,但是注意,如果没有设置值的话,会报错。

package com.bobo.controller;

import com.bobo.model.User;
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.SessionAttributes;

@Controller
@SessionAttributes("u")
public class ModelAttributeController {

    Object o1 = null;
    Object o2 = null;
    Object o3 = null;

    @RequestMapping("/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("u") User user, Model model) {
        System.out.println("testModelAttribute" + user);
        o3 = model;
        //可以看到所有的model都是同一个对象
        System.out.println(o1 == o3);
        //可以看到存储的user对象也是同一个
        System.out.println(o2 == user);
        return "success";
    }

    @ModelAttribute
    public void MyModelAttribute(Model model) {
        o1 = model;
        User user = new User();
        user.setId(100);
        user.setName("bobo");
        user.setPassword("123");
        user.setAge(12);
        model.addAttribute("u", user);
        o2 = user;
        System.out.println("MyModelAttribute:" + user);
    }
}


注意:ModelAttribute除了可以使用设置值到model中之外,还可以利用返回值。

package com.bobo.controller;

import com.bobo.model.User;
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.SessionAttributes;

@Controller
public class ModelAttributeController {

    Object o1 = null;
    Object o2 = null;
    Object o3 = null;

    @RequestMapping("/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("u") User user, Model model) {
        System.out.println("testModelAttribute" + user);
        o3 = model;
        //可以看到所有的model都是同一个对象
        System.out.println(o1 == o3);
        //可以看到存储的user对象也是同一个
        System.out.println(o2 == user);
        return "success";
    }

    @ModelAttribute("u")
    public User MyModelAttribute(Model model) {
        o1 = model;
        User user = new User();
        user.setId(100);
        user.setName("bobo");
        user.setPassword("123");
        user.setAge(12);
//        model.addAttribute("u", user);
        o2 = user;
        System.out.println("MyModelAttribute:" + user);
        return user;
    }
}


8、使用forward实现页面转发

在发送请求的时候,可以通过forward:来实现转发请求的功能:

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ForwardController {

    @RequestMapping("/success")
    public String testForward(){
        System.out.println(0);
        return "success";
    }

    @RequestMapping("/forward01")
    public String testForward01(){
        System.out.println(1);
        return "forward:/success";
    }

    @RequestMapping("/forward02")
    public String testForward02(){
        System.out.println(2);
        return "forward:/forward01";
    }
}

9、使用redirect来实现重定向

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ForwardController {

    @RequestMapping("/success")
    public String testForward(){
        System.out.println(0);
        return "success";
    }

    @RequestMapping("/forward01")
    public String testForward01(){
        System.out.println(1);
        return "redirect:/success";
    }

    @RequestMapping("/forward02")
    public String testForward02(){
        System.out.println(2);
        return "redirect:/forward01";
    }
}

在javaweb的时候大家应该都接触过重定向和转发的区别,下面再详细说一下:

转发:

​ 由服务器的页面进行跳转,不需要客户端重新发送请求:

​ 特点如下:

​ 1、地址栏的请求不会发生变化,显示的还是第一次请求的地址

​ 2、请求的次数,有且仅有一次请求

​ 3、请求域中的数据不会丢失

​ 4、根目录:localhost:8080/项目地址/,包含了项目的访问地址

重定向:

​ 在浏览器端进行页面的跳转,需要发送两次请求(第一次是人为的,第二次是自动的)

​ 特点如下:

​ 1、地址栏的地址发生变化,显示最新发送请求的地址

​ 2、请求次数:2次

​ 3、请求域中的数据会丢失,因为是不同的请求

​ 4、根目录:localhost:8080/ 不包含项目的名称

对比:

区别 转发forward() 重定向sendRedirect()
根目录 包含项目访问地址 没有项目访问地址
地址栏 不会发生变化 会发生变化
哪里跳转 服务器端进行的跳转 浏览器端进行的跳转
请求域中数据 不会丢失 会丢失

10、静态资源的访问

当页面中包含静态资源的时候我们能够正确的获取到吗?
hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    pageContext.setAttribute("ctx",request.getContextPath());
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
hello springmvc
<img src="${ctx}/images/timg.jpg">
</body>
</html>

此时大家发现我们请求的图片根本访问不到,根据查看发现路径是没有问题的,那么为什么会找不到静态资源呢?

大家发现此时是找不到对应的mapping映射的,此时是因为DispatcherServlet会拦截所有的请求,而此时我们没有对应图片的请求处理方法,所以此时报错了,想要解决的话非常简单,只需要添加一个配置即可

<!--
此配置表示  我们自己配置的请求由controller来处理,但是不能请求的处理交由tomcat来处理
静态资源可以访问,但是动态请求无法访问
-->
<mvc:default-servlet-handler/>

但是加上此配置之后,大家又发现此时除了静态资源无法访问之外,我们正常的请求也无法获取了,因此还需要再添加另外的配置:

<!--保证静态资源和动态请求都能够访问-->
<mvc:annotation-driven/>

二、自定义视图解析器

我们在之前的操作中已经用了SpringMVC中提供的视图解析器,那么如果我们需要实现自己的视图解析器该如何操作呢?


ResolverController

package com.bobo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ResolverController {
    @RequestMapping("/testMyView")
    public String testResolver(Model model) {
        model.addAttribute("msg", "波波");
        return "zsb:/success";
    }
}

MyViewResolver

package com.bobo.resolver;

import com.bobo.view.MyView;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import java.util.Locale;

@Controller
public class MyViewResolver implements ViewResolver {

    private int order;

    public void setOrder(int order) {
        this.order = order;
    }

    public int getOrder() {
        return order;
    }

    public View resolveViewName(String viewName, Locale locale) throws Exception {
        if (viewName.startsWith("zsb:")) {
            return new MyView();
        } else {
            return null;
        }
    }
}

MyView

package com.bobo.view;

import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class MyView implements View {
    public String getContentType() {
        return "text/html";
    }

    public void render(Map<String, ?> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("保存的对象是map:"+map);
        httpServletResponse.setContentType("text/html");
        httpServletResponse.getWriter().write("欢迎你");
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.bobo"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/page/"></property>
    </bean>
    <bean class="com.bobo.resolver.MyViewResolver">
        <property name="order" value="1"></property>
    </bean>
</beans>

运行输出:

保存的对象是map:{msg=波波}

二、自定义类型转换器

在日常的企业开发需求中,我们输入文本框的内容全部都是字符串类型,但是在后端处理的时候我们可以用其他基本类型来接受数据,也可以使用实体类来接受参数,这个是怎么完成的呢?就是通过SpringMVC提供的类型转换器,SpringMVC内部提供了非常丰富的类型转换器的支持,但是有些情况下有可能难以满足我们的需求,因此需要我们自己实现,如下:

User.java

package com.bobo.model;

public class User {
    private Integer id;
    private String name;
    private String password;
    private Integer age;

    public User() {
    }

    public User(Integer id, String name, String password, Integer age) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

MyConverter.java

package com.bobo.converter;

import com.bobo.model.User;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class MyConverter implements Converter<String, User> {
    public User convert(String s) {
        System.out.println("--------");
        User user = null;
        if (s != null && s.split("-").length == 4) {
            user = new User();
            String[] textArray = s.split("-");
            user.setId(Integer.valueOf(textArray[0]));
            user.setName(textArray[1]);
            user.setPassword(textArray[2]);
            user.setAge(Integer.valueOf(textArray[3]));
        }
        return user;
    }
}

ConverterController.java

package com.bobo.controller;

import com.bobo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ConverterController {
    @RequestMapping("testConverterUser")
    public String testConverter(User user, Model model){
        System.out.println(user);
        model.addAttribute("user",user);
        return "success";
    }
}

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setAttribute("ctp",request.getContextPath());
%>
<form action="${ctp}/testConverterUser" method="post">
    格式:id-name-password-age</br>
    <input type="text" name="user"/></br>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

application_context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.bobo"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/page/"></property>
    </bean>
    <bean class="com.bobo.resolver.MyViewResolver">
        <property name="order" value="1"></property>
    </bean>
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="myConverter"></ref>
            </set>
        </property>
    </bean>
</beans>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
user:${user}
</body>
</html>

运行输出

四、自定义日期格式化转换器

有时候我们经常需要在页面添加日期等相关信息,此时需要制定日期格式化转换器,此操作非常简单:只需要在单独的属性上添加@DateTimeFormat注解即可,制定对应的格式

User.java

package com.bobo.model;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class User {
    private Integer id;
    private String name;
    private String password;
    private Integer age;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birth;
    public User() {
    }

    public User(Integer id, String name, String password, Integer age, Date birth) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.age = age;
        this.birth = birth;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                '}';
    }
}

dateFormatController.java

package com.bobo.controller;

import com.bobo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DateFormatController {
    @RequestMapping("/testDateFormat")
    public String testDateFormat(User user) {
        System.out.println(user);
        return "success";
    }
}

dateFormat.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<%
    request.setAttribute("ctp", request.getContextPath());
%>
<body>
<form action="${ctp}/testDateFormat">
    id:<input type="text" name="id"/><br>
    name:<input type="text" name="name"/><br>
    password:<input type="text" name="password"/><br>
    age:<input type="text" name="age"/><br>
    date:<input type="text" name="birth"/><br>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

application_context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.bobo"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/page/"></property>
    </bean>
    <bean class="com.bobo.resolver.MyViewResolver">
        <property name="order" value="1"></property>
    </bean>
    <!--此配置表示  我们自己配置的请求由controller来处理,但是不能请求的处理交由tomcat来处理
    静态资源可以访问,但是动态请求无法访问
    -->
    <mvc:default-servlet-handler/>
    <!--保证静态资源和动态请求都能够访问-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>-->
<!--    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">-->
<!--        <property name="converters">-->
<!--            <set>-->
<!--                <ref bean="myConverter"></ref>-->
<!--            </set>-->
<!--        </property>-->
<!--    </bean>-->
</beans>

输入一个错误格式日期如下:

提交请求报错:

输入正确格式日期:

提交成功

但是需要注意的是,如果同时配置了自定义类型转换器之后,那么日期格式转化是有问题的。

application_context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.bobo"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/page/"></property>
    </bean>
    <bean class="com.bobo.resolver.MyViewResolver">
        <property name="order" value="1"></property>
    </bean>
    <!--此配置表示  我们自己配置的请求由controller来处理,但是不能请求的处理交由tomcat来处理
    静态资源可以访问,但是动态请求无法访问
    -->
    <mvc:default-servlet-handler/>
    <!--保证静态资源和动态请求都能够访问-->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="myConverter"></ref>
            </set>
        </property>
    </bean>
</beans>

原因就在于ConversionServiceFactoryBean对象中有且仅有一个属性converters,此时可以使用另外一个类来做替换FormattingConversionServiceFactoryBean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.bobo"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/page/"></property>
    </bean>
    <bean class="com.bobo.resolver.MyViewResolver">
        <property name="order" value="1"></property>
    </bean>
    <!--此配置表示  我们自己配置的请求由controller来处理,但是不能请求的处理交由tomcat来处理
    静态资源可以访问,但是动态请求无法访问
    -->
    <mvc:default-servlet-handler/>
    <!--保证静态资源和动态请求都能够访问-->
<!--    <mvc:annotation-driven></mvc:annotation-driven>-->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="myConverter"></ref>
            </set>
        </property>
    </bean>
</beans>

五、数据校验

一般情况下我们会在前端页面实现数据的校验,但是大家需要注意的是前端校验会存在数据的不安全问题,因此一般情况下我们都会使用前端校验+后端校验的方式,这样的话既能够满足用户的体验度,同时也能保证数据的安全,下面来说一下在springmvc中如何进行后端数据校验。

JSR303是 Java 为 Bean 数据合法性校验提供的标准框架,它已经包含在 JavaEE 6.0 中 。JSR 303 (Java Specification Requests意思是Java 规范提案)通过 Bean 属性上标注类似于 @NotNull、@Max 等标准的注解指定校验规则,并通过标准的验证接口对 Bean 进行验证。

JSR303:

Hibernate Validator 扩展注解:

spring中拥有自己的数据校验框架,同时支持JSR303标准的校验框架,可以在通过添加注解的方式进行数据校验。在spring中本身没有提供JSR303的实现,需要导入依赖的包。

		<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.0.Final</version>
        </dependency>

pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bobo</groupId>
    <artifactId>my_spring_mvc02</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
    </dependencies>

</project>

注意我们用idea开发时,必须要把新加入的hibernate-validator包放到我们lib下面,否则不生效。

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    pageContext.setAttribute("ctp",request.getContextPath());
%>
    <form action="${ctp}/testValidate" method="post">
        编号:<input type="text" name="id"><br>
        姓名:<input type="text" name="name"><br>
        年龄:<input type="text" name="age"><br>
        性别:<input type="text" name="gender"><br>
        日期:<input type="text" name="birth"><br>
        邮箱:<input type="text" name="email"><br>
        <input type="submit" value="提交">
</form>
</body>
</html>

User.java

package com.bobo.model;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import java.util.Date;

public class User {
    private Integer id;
    @NotNull
    @Length(min = 5,max = 10)
    private String name;
    private Integer age;
    private String gender;
    @Past
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birth;
    @Email
    private String email;
    public User() {
    }

    public User(Integer id, String name, String gender, Integer age, Date birth, String email) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.birth = birth;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public String getEmail() {
        return email;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + gender + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", email='" + email + '\'' +
                '}';
    }
}

DataValidateController.java

package com.bobo.controller;

import com.bobo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
public class DataValidateController {

    @RequestMapping("/testValidate")
    public String testValidate(@Valid User user, BindingResult bindingResult){
        System.out.println(user);
        System.out.println(bindingResult);
        if (bindingResult.hasErrors()){
            System.out.println("验证失败");
            return "redirect:/login.jsp";
        }else {
            System.out.println("验证成功");
            return "success";
        }
    }
}

application_context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.bobo"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/page/"></property>
    </bean>
    <bean class="com.bobo.resolver.MyViewResolver">
        <property name="order" value="1"></property>
    </bean>
    <!--此配置表示  我们自己配置的请求由controller来处理,但是不能请求的处理交由tomcat来处理
    静态资源可以访问,但是动态请求无法访问
    -->
    <mvc:default-servlet-handler/>
    <!--保证静态资源和动态请求都能够访问-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>-->
<!--    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">-->
<!--        <property name="converters">-->
<!--            <set>-->
<!--                <ref bean="myConverter"></ref>-->
<!--            </set>-->
<!--        </property>-->
<!--    </bean>-->
</beans>

此时大家发现在报错的地方无法出现错误提示,可以换另外一种方式:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application_context.xml</param-value>
    </context-param>
    <filter>
        <filter-name>encoding</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application_context.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

DataValidateController.java

package com.bobo.controller;

import com.bobo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
public class DataValidateController {

    @RequestMapping("/testValidate")
    public String testValidate(@Valid User user, BindingResult bindingResult){
        System.out.println(user);
        System.out.println(bindingResult);
        if (bindingResult.hasErrors()){
            System.out.println("验证失败");
            return "add";
        }else {
            System.out.println("验证成功");
            return "success";
        }
    }

    @RequestMapping("add")
    public String add(Model model){
        model.addAttribute("user",new User(1,"zhangsan","女",12,null,"1234@qq.com"));
        return "add";
    }
}

add.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    pageContext.setAttribute("ctp", request.getContextPath());
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form:form action="${ctp}/testValidate" method="post" modelAttribute="user">
    编号:<form:input path="id"></form:input> <form:errors path="id"></form:errors><br>
    姓名:<form:input path="name"></form:input> <form:errors path="name"></form:errors><br>
    年龄:<form:input path="age"></form:input> <form:errors path="age"></form:errors><br>
    性别:<form:input path="gender"></form:input> <form:errors path="gender"></form:errors><br>
    日期:<form:input path="birth"></form:input> <form:errors path="birth"></form:errors><br>
    邮箱:<form:input path="email"></form:input> <form:errors path="email"></form:errors><br>
    <input type="submit" value="提交">
</form:form>
</body>
</html>

原生的表单如何获取错误信息:

@Controller
public class DataValidateController {
    @RequestMapping("/dataValidate")
    public String validate(@Valid User user, BindingResult bindingResult, Model model) {
        System.out.println(user);
        Map<String,Object> errorsMap = new HashMap<String, Object>();
        if (bindingResult.hasErrors()) {
            System.out.println("验证失败");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                System.out.println(fieldError.getDefaultMessage());
                System.out.println(fieldError.getField());
                errorsMap.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            model.addAttribute("errorInfo",errorsMap);
            return "add";
        } else {
            System.out.println("验证成功");
            return "hello";
        }
    }

    @RequestMapping("add")
    public String add(Model model){
        model.addAttribute("user",new User(1,"zhangsan",12,"女",null,"1234@qq.com"));
        return "add";
    }
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<form:form action="dataValidate"  modelAttribute="user" method="post">
    编号:<form:input path="id"></form:input><form:errors path="id"></form:errors>--->${errorInfo.id} <br/>
    姓名:<form:input path="name"></form:input><form:errors path="name"></form:errors>--->${errorInfo.name}<br/>
    年龄:<form:input path="age"></form:input><form:errors path="age"></form:errors>--->${errorInfo.age}<br/>
    性别:<form:input path="gender"></form:input><form:errors path="gender"></form:errors>--->${errorInfo.gender}<br/>
    生日:<form:input path="birth"></form:input><form:errors path="birth"></form:errors>--->${errorInfobirth}<br/>
    邮箱:<form:input path="email"></form:input><form:errors path="email"></form:errors>--->${errorInfo.email}<br/>
    <input type="submit" value="submit">
</form:form>
</body>
</html>

转载:https://blog.csdn.net/u013277209/article/details/105352957
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场