目录
一、接受简单的参数
先引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
</dependencies>
在输入框直接传参
控制台已经输出我们传的内容了 会自动匹配参数
二、实体类接参
创建一个实体类user
创建jsp接参
放行静态资源及处理其他的
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
三、接受json参数
package com.exy.controller;
import com.exy.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
/**
* @program: SpringMVCFirst01
* @description:
* @author: jdy
* @create: 2021-12-09 20:34
**/
@Controller
public class Josn01 {
@RequestMapping("json02")
@ResponseBody
public User json02(){
User user = new User("jst1","a","b",new Date());
return user;
}
}
list形式的
@RequestMapping("json01")
@ResponseBody
public List<User> json01(){
System.out.println(1);
List<User> list = new ArrayList<User>();
User user = new User("jst1","a","b",new Date());
User user2 = new User("jst2","a","b",new Date());
User user3 = new User("jst3","a","b",new Date());
list.add(user);
list.add(user2);
list.add(user3);
System.out.println(list);
return list;
}