package com.shsxt.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;
/**
* 设置request请求域
* 1.ModelAndView对象
* modelAndView.addObject("变量名","值")
* 2.HttpServletRequest 对象(形参上定义)
* setAttribute("键","值")
* 3.Model对象(形参上定义)
* addAttribute("键","值")
* 4.ModelMap对象(形参上定义)
* addAttribute("键","值")
* 5.Map对象
* put("键","值")
*
* 设置session作用域
* HttpServletRequest 对象(形参上定义)//对象就是session
* session.setAttribute("键","值")
*
*
* addObject("变量名","值")
* 2.HttpServletRequest对象
*/
@Controller
public class ModelController {
@RequestMapping("m01")
public ModelAndView model01() {
ModelAndView modelAndView = new ModelAndView();
//设置请求域
modelAndView.addObject("msg", "请求域");
//设置视图
modelAndView.setViewName("model");
return modelAndView;
}
/**
* HttpServletRequest对象
* setAttribute
* @param request
* @return
*/
@RequestMapping("m02")
public String model02(HttpServletRequest request) {
request.setAttribute("msg", "request作用域");
return "model";
}
/**
* Model对象
* addAttribute
* @param model
* @return
*/
@RequestMapping("m03")
public String model03(Model model) {
model.addAttribute("msg","model请求域");
return "model";
}
/**
* ModelMap对象
* addAttribute
* @param model
* @return
*/
@RequestMapping("m04")
public String model04(ModelMap model) {
model.addAttribute("msg","modelMap请求域");
return "model";
}
/**
* Map 对象
* @param map
* @return
*/
@RequestMapping("m05")
public String model05(Map map) {
map.put("msg","map请求域");
return "model";
}
}
版权声明:本文为m0_52946104原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。