一、获取请求参数
1.原生serveltAPI
@Controller
public class TestController{
@RequestMapping("/test01")
public String test01(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username : " + username + ", password : " + password);
return "success";
}
}
2.控制器参数列表
@Controller
public class TestController{
// 当请求参数与参数列表名一致时,会自动给形参赋值。
@RequestMapping("/test01")
public String test01(String username,String password) {
System.out.println("username : " + username + ", password : " + password);
return "success";
}
}
3.@Requestparam
@Controller
public class TestController{
// 当请求参数与形参列表名不一致时,使用@Requestparam注解
@RequestMapping("/test01")
public String test01(@RequestParam("username") String user_name, @Requestparam("password") String pass_word) {
System.out.println("username : " + user_name + ", password : " + pass_word);
return "success";
}
}
4.@RequestHeader 获取请求头
@Controller
public class TestController{
@RequestMapping("/test01")
public String test01(@RequestHader("Host") String location) {
System.out.println("Host : " + locaiont);
return "success";
}
}
5.@CookieValue 获取cookie存储的值
@Controller
public class TestController{
@RequestMapping("/test01")
public String test01(@CookieValue("JSESSIONID") String JSESSIONID) {
System.out.println("JSESSIONID : " + JSESSIONID);
return "success";
}
}
6.实体类
@Controller
public class TestController{
// 当请求参数与java实体类的属性一致,会将请求参数自动生成实体类
@RequestMapping("/test01")
public String test01(User user) {
System.out.println(user);
return "success";
}
}
二、作用域
四大作用域 :
1.page (淘汰)
2.request
3.session
4.application
向request域中存放数据
1.使用原生servletAPI
@Controller
public class TestController{
@RequestMapping("/test01")
public String test01(HttpServeltRequest request) {
request.setAttribute("msg","servlet");
return "success";
}
}
2.使用ModelAndView
@Controller
public class TestController{
@RequestMapping("/test01")
public ModelAndView test01() {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ModelAndView");
mv.setViewName("success");
return mv;
}
}
3.使用Model
@Controller
public class TestController{
@RequestMapping("/test01")
public String test01(Model model) {
model.addAttribute("msg","model");
return "success";
}
}
4.使用ModelMap
@Controller
public class TestController{
@RequestMapping("/test01")
public String test01(ModelMap map) {
map.addAttribute("msg","ModelMap");
return "success";
}
}
5.使用Map
@Controller
public class TestController{
@RequestMapping("/test01")
public String test01(Map<String,String> map) {
map.put("msg","map");
return "success";
}
}
向session作用域存放数据
1.原生servlet
@Controller
public class TestController{
@RequestMapping("/test03")
public String test03(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("msg","session");
return "success";
}
}
向application作用域存放数据
1.原生servlet
@Controller
public class TestController{
@RequestMapping("/test03")
public String test04(HttpServletRequest request) {
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
application.setAttribute("msg","application");
return "success";
}
}