一、向request域对象共享数据的5种方式
(1)servletAPI
(2)ModelAndView
(3)Model
(4)Map
(5)ModelMap
package zj.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* 概述:
* 作者:zhujie
* 创建时间:2021/11/11 14:02
*/
@Controller
public class TestConreoller {
@RequestMapping("/")
public String index(){
return "index";
}
/**
* 概述:servletAPI测试向request域对象共享数据
*/
@RequestMapping("/test")
public String test(HttpServletRequest request){
request.setAttribute("test","servletAPI");
return "success";
}
/**
* 概述:ModelAndView测试向request域对象共享数据
*/
@RequestMapping("/modelAndView")
public ModelAndView modelAndView(HttpServletRequest request){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("test","ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
/**
* 概述:Model测试向request域对象共享数据
*/
@RequestMapping("/model")
public String model(Model model){
model.addAttribute("test","Model");
return "success";
}
/**
* 概述:Map测试向request域对象共享数据
*/
@RequestMapping("/map")
public String map(Map<String,Object> map){
map.put("test","Map");
return "success";
}
/**
* 概述:ModelMap测试向request域对象共享数据
*/
@RequestMapping("/modelMap")
public String modelMap(ModelMap modelMap){
modelMap.addAttribute("test","ModelMap");
return "success";
}
}