文章目录

  • 1.SpringMVC的几个注解说明
  • 2.过滤静态资源设置
  • 3.中文乱码过滤处理设置
  • 4.获取请求数据
  • 方式(1) 若前端传输的数据参数和后端处理方法中的方法参数一致
  • 方式(2) 若前端传输的数据参数和后端处理方法中的方法参数不一致
  • 方式(3)使用httpservletrequest接收数据;
  • 方式(4)将请求数据的参数与实体类的属性进行绑定



1.SpringMVC的几个注解说明

首先是在类上写的@Controller;或者@RestController;仅能作用类;
标注这个类为spring生成这个类的对象;且为控制层;


注解@RequestMapping处理请求地址映射;可以写在类上,或者写在方法上,写在类上的话,
这个注解写在类上的话,在整个项目中不能重复;
这个注解写在方法上时,在同一个类中,不能重复;

键入注解,可看到;valuepath属性可以设置一组映射路径;

springmvc 非注解方式 springmvc中的注解有哪些_springmvc

value 就是请求的路径;径相对于应用的上下文;和path是一样的;

//这个path或者value中可以配置一组路径
 @RequestMapping(path = {"test0","test1","test3"})
 public void test1(){
     System.out.println("测试1");
 }

在注解@RequestMapping的method属性;可配置请求的方式,比如GET/POST

//可用method属性标明请求的方式;
@RequestMapping(path = "/test1s",method = RequestMethod.POST)
public void test2(){
    System.out.println("测试2");
}

当然,如果不想用method进行标注的话,实际也可
使用注解@GetMapping;@PostMapping来匹配相应的请求方式;

@GetMapping(path = "/findU")
    public void test3(){

    }

    @PostMapping(path = "/addU")
    public void test4(){

    }

2.过滤静态资源设置

为避免踩坑,这里提前说下怎么配置静态资源过滤吧;

<mvc:default-servlet-handler/>

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--开启springmvc注解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!--过滤静态资源-->
    <mvc:default-servlet-handler/>

</beans>

当 DispatcherServlet 的 url 配置为/时 需要添加此配置,能够访问静态资源
例如.jpg,.js,.css 带有后缀名文件。
在 springMVC-servlet.xml 中配置<mvc:default-servlet-handler />后,会在 Spring MVC 上下文中定义
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它会像一个检查员,对进入 DispatcherServlet 的 URL 进行筛查,
如果发现是静态资源的请求,就将该请求转由 Web 应用服务器默认的
Servlet 处理,
如果不是静态资源的请求,才由 DispatcherServlet 继续处理。

3.中文乱码过滤处理设置

之前在学习web时,自定义写过中文乱码过滤器,
springmvc提供了这项设置;配置即可使用;
web.xml下配置

<filter>
    <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

其中还有初始化参数encoding

springmvc 非注解方式 springmvc中的注解有哪些_html_02

4.获取请求数据

用案例来说明不同的获取方式;

方式(1) 若前端传输的数据参数和后端处理方法中的方法参数一致

参数名一致时,直接在处理的方法处,用对应的方法参数取值即可;

案例测试 先写个简易html页面index.html;这里就放一个链接

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<a href="person/getPerson?id=12&name=xiaozhire0&age=22">这是一个链接</a>
</body>
</html>

处理的controller层

@RestController
@RequestMapping(path = "/person")
public class PersonController {
    //接收数据测试;
    //方式1:若前端传输的数据参数和后端处理方法中的方法参数一致;
    //我这里直接就用了传递的id,name,age同名的;
    @GetMapping("/getPerson")
    public void getPerson1(Integer id,String name,Integer age){
        //收到获取的数据;
        System.out.println("获取Id-->"+id);
        System.out.println("获取姓名-->"+name);
        System.out.println("获取年龄-->"+age);
    }
}

访问http://localhost:8080/ssmdemobyxiaozhi/index.html

springmvc 非注解方式 springmvc中的注解有哪些_html_03

点击链接,已经获取到了数据

springmvc 非注解方式 springmvc中的注解有哪些_springmvc 非注解方式_04


方式(2) 若前端传输的数据参数和后端处理方法中的方法参数不一致

这时就要在处理的方法参数前使用注解@RequestParam("前端的传输参数名");还可以用注解的方式获取请求头中的参数;例如@RequestHeader("User-Agent") String UG 首先看看,要是参数名不一致,我用方式1还行不;

不一致,已经获取不到了;

springmvc 非注解方式 springmvc中的注解有哪些_System_05

案例测试 搭建简易页面,index.html;就存入链接;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<a href="person/getPerson?id=99&name=xiaozhire0s&age=22">这是一个链接</a>
</body>
</html>

controller控制层处理;

@RestController
@RequestMapping(path = "/person")
public class PersonController {
    //接收数据测试;
    //方式2:
    @GetMapping("/getPerson")
    public void getPerson1(@RequestParam("id")   Integer ids,
                           @RequestParam("name") String  namesdasda,
                           @RequestParam("age")  String  agesdaa,
                           @RequestHeader("User-Agent")String user_agent){
        //收到获取的数据;
        System.out.println("获取Id-->"+ids);
        System.out.println("获取姓名-->"+namesdasda);
        System.out.println("获取年龄-->"+agesdaa);
        System.out.println("请求头参数->"+user_agent);
    }
}

访问,点击链接,可直接获取

springmvc 非注解方式 springmvc中的注解有哪些_System_06


方式(3)使用httpservletrequest接收数据;

这个方法的话,需要在pom.xml文件下导入servlet的maven包

<!--servlet-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>

然后,和以前刚开始学web一样,用request请求的getParameter()方法获取指定的参数;但是这种方式的话,获取的值会被转为字符串;而且要是数据量大的话,要写多个这样的获取方法getParameter();是比较麻烦的;
案例展示 这里的话,就用post方式提交了,
写个简易的index.html,来面放个简易表单

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是个简易的表单
<form action="person/getPerson" method="post">
    <input type="text" name="id"/><br/>
    <input type="text" name="name"/><br/>
    <input type="text" name="age"/><br/>
    <input type="submit" value="提交">
</form>
</body>
</html>

大概效果这样

springmvc 非注解方式 springmvc中的注解有哪些_springmvc_07

controller层处理的方法

@RestController
@RequestMapping(path = "/person")
public class PersonController {
    //方式3:
    @PostMapping("/getPerson")
    public void getPerson3(HttpServletRequest request){
        //用request的方法获取参数;
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        System.out.println("获取Id-->"+id);
        System.out.println("获取姓名-->"+name);
        System.out.println("获取年龄-->"+age);
    }
}

启动测试.提交表单可获取数据

springmvc 非注解方式 springmvc中的注解有哪些_System_08


方式(4)将请求数据的参数与实体类的属性进行绑定

注意,提交的数据的参数名要和实体类的属性名一致;不然封装不到数据;
注意,日期类型的属性,在实体类中要用注解@DateTimeFormat(pattern = "YYYY-MM-dd"),
进行标注(格式);不然有错误;

案例搭建;
这里写个模拟表单提交,向数据库中添加数据的案例;
实体类的话就用之前的这个案例的实体类;People;这里日期类型的加了注解;

//将这个类注入到spring;
@Component
public class People {
    //这边的话,就保持和数据库的字段名一致吧;
    private Integer id;
    private String  name;
    private String  password;
    private Integer age;
    @DateTimeFormat(pattern = "YYYY-MM-dd")
    private Date    birthday;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String 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 getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

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

提交的简易表单

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是个添加人类信息的表单
<form action="person/addPerson" method="post">
    姓名:<input type="text" name="name"/><br/>
    密码:<input type="text" name="password"/><br/>
    年龄:<input type="text" name="age"/><br/>
    生日:<input type="text" name="birthday"/><br/>
    <input type="submit" value="提交">
</form>
</body>
</html>

控制层处理

@RestController
@RequestMapping(path = "/person")
public class PersonController {
    @Autowired
    private PersonService personService;

    //方式4:将请求的参数与实体类的属性进行绑定;
    @PostMapping("/addPerson")
    public void addPerson(People people){
        System.out.println("这个添加的用户->"+people);
        //调用方法,直接添加;
        personService.savePeople(people);
    }
}

执行测试;

springmvc 非注解方式 springmvc中的注解有哪些_html_09

控制台日志信息显示有添加行为

springmvc 非注解方式 springmvc中的注解有哪些_springmvc_10

数据完成提交

springmvc 非注解方式 springmvc中的注解有哪些_System_11

既然是案例演示,这里就打上断点,看看这边的处理情况;
cglib代理

springmvc 非注解方式 springmvc中的注解有哪些_html_12

再看看日期不用注解标注,会出现啥情况;

先把注解去了

springmvc 非注解方式 springmvc中的注解有哪些_html_13

springmvc 非注解方式 springmvc中的注解有哪些_spring_14

出现错误

springmvc 非注解方式 springmvc中的注解有哪些_spring_15

仔细看这段错误,他说是字符串转换日期格式有问题;也就是我这用text请求发出的是字符串;

springmvc 非注解方式 springmvc中的注解有哪些_springmvc_16