Four scopes in the controller

request、session、application

  • The parameter is HttpServletRequest to get the request
  • The session can be obtained through request, and the session can also be obtained by setting parameters
  • Application can be obtained through request, application is not allowed to be obtained through parameters
@RequestMapping("demo1")
	public String demo(HttpServletRequest abc){
    
    
		abc.setAttribute("a", "a的值");
		HttpSession session = abc.getSession();
		session.setAttribute("b", "b的值");
		
		ServletContext application = abc.getServletContext();
		application.setAttribute("c", "c的值");
		return "index";
	}

Other variants of request

The parameter is set to map, put a key-value pair, actually use request.setAttribute

@RequestMapping("demo2")
	public String demo2(Map<String,Object>map){
    
    
		map.put("map", "map的值");
		return "index";
	}

The underlying scope is still a request, changing the soup without changing the medicine

@RequestMapping("demo3")
	public String demo3(Model model){
    
    
		model.addAttribute("model", "model的值");
		return "index";
	}

It is still a request, but the view function is added, the return value type is ModelAndView, the jsp of the jump is set when the object is created, addObject is equivalent to req.setAttribute

@RequestMapping("demo4")
	public ModelAndView demo4(){
    
    
		ModelAndView mav=new ModelAndView("index");
		mav.addObject("mav", "mav的值");
		return mav;
	}

Review the value in the scope in jsp

When taking out each key-value pair below, you don’t need to add xxxScope, and add it to indicate from which scope it is taken out. If not, it will search by name, start from the smallest scope, and stop when it finds one.

	request:${requestScope.a}<br>
	session:${sessionScope.b}<br>
	application:${applicationScope.c}<br> 
	map:${requestScope.map}<br> 
	model:${requestScope.model }<br> 
	modelAndView:${requestScope.mav}<br> 

Guess you like

Origin blog.csdn.net/WA_MC/article/details/113108499