package test.mvc._04_request;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by thinkpad on 2019/9/9.
*/
@Controller
public class RequestController {
//日期类型处理
//在对象字段或Controller形参上添加上@DateTimeFormat(pattern="yyyy-MM-dd")
//employee加了@DateTimeFormat(pattern="yyyy-MM-dd")
@RequestMapping("/test7")
public ModelAndView test7(Employee employee){
System.out.println(employee.getBirthday());
return null;
}
@RequestMapping("/test6")
public ModelAndView test6(@DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday){
System.out.println(birthday);
return null;
}
/*情况五:RESTful是一种软件架构风格,严格上说是一种编码风格,其充分利用 HTTP 协议本身语义从而提供了一组设计原则和约束条件。
主要用于客户端和服务器交互类的软件,该风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。*/
@RequestMapping("/employee/{id}/{deptId}")
public ModelAndView delete(@PathVariable("id") Integer employeeId,@PathVariable("deptId")Integer deptId){
System.out.println(employeeId);
System.out.println(deptId);
return null;
}
/*情况四:数组和List集合类型参数.
参数名相同,参数值不同的多个参数,
可以直接封装到方法的数组类型的形参中,
也可以直接封装到对象中的集合属性中
接收List类型参数(不能直接获取,只能通过对象封装List集合)*/
@RequestMapping("/test5")
public ModelAndView test5(Employee employee){
System.out.println(employee.getId().size());
for (String i : employee.getId()) {
System.out.println(i);
}
return null;
}
//接收数组类型参数
@RequestMapping("/test4")
/*
对于参数名相同的多个请求参数,可以直接使用数组作为方法的形参接收
可以使用对象中的集合属性接收
*/
public ModelAndView test4(String id[]){
for (String i : id) {
System.out.println(i);
}
return null;
}
/*情况三:对象传参,能够自动把请求参数封装到声明在形参上的对象中,此时请求参数必须和对象的属性同名(使用比较多).
注意:此时,SpringMVC会将对象参数直接放入request的作用域中,名称为类型首字母小写
@ModelAttribute作用如下:
1、设置请求参数绑定到Model对象中并传到视图页面的key名.
2、将方法返回值或请求参数绑定到Model对象中并传到视图页面,设置key名.*/
@RequestMapping("/test3")
/*
可以使用对象作为方法的形参,同时接受接受前台的多个请求参数
SpringMVC会基于同名匹配,将请求参数的值注入对应的对象中的属性中
*/
public ModelAndView test3(@ModelAttribute("em") Employee employee){
System.out.println(employee);
ModelAndView mv = new ModelAndView();
mv.setViewName("request/request");
return mv;
}
/*情况二:简单类型参数和RequestParam注解(使用比较多).
一:如果请求参数和Controller方法的形参同名. 可以直接接收.
二:如果请求参数和Controller方法的形参不同名. 使用@RequestParam注解贴在形参前,设置对应的请求参数名称.*/
@RequestMapping("/test2")
/*
简单数据类型的参数,可以直接使用方法的形参来接受,基于同名匹配原则
@RequestParam注解可以指定形参获取请求参数中哪个请求参数的值
补充:凡是标注了@RequestParam注解的属性,请求时就必须要传递请求参数
required:表示是否必须要传值
defaultValue:当没有该请求参数时,SpringMVC给请求参数的默认值.
*/
public ModelAndView test2(@RequestParam("username") String name,
@RequestParam(defaultValue = "12345") String pass){
System.out.println(name);
System.out.println(pass);
return null;
}
//情况一:最传统的方式,方法参数中带入request,通过request.getParameter("参数名"),再封装到JavaBean中
@RequestMapping("/test1")
public ModelAndView test1(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
return null;
}
}
Employee.java:
package test.mvc._04_request;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import java.util.List;
/**
* Created by thinkpad on 2019/9/9.
*/
@Setter
@Getter
@ToString
public class Employee {
private String username;
private String password;
private List<String> id;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
}

本文详细介绍SpringMVC中各种请求参数的处理方式,包括简单类型、数组、List集合、对象传参、@RequestParam和@ModelAttribute注解的使用,以及RESTful风格的路径变量。
890

被折叠的 条评论
为什么被折叠?



