(六)springmvc之ModelAndView、Model、Map、ModelMap

<a href="<%=request.getContextPath()%>/responseData/response_1">使用原生的作用域</a>
    
    <a href="<%=request.getContextPath()%>/responseData/response_2">ModelAndView</a>
    
    <a href="<%=request.getContextPath()%>/responseData/response_3">Model,Map,ModelMap</a>

controller

package com.shyroke.controller;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
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;

@Controller
@RequestMapping(value = "/responseData")
public class ResponseDataController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HttpSession session;
    @Autowired
    private ServletContext servletContext;

    @RequestMapping(value = "/response_1")
    public String response_1() throws Exception {
        this.request.setAttribute("req_key", "Request作用域");

        this.session.setAttribute("session_key", "Session作用域");

        this.servletContext.setAttribute("app_key", "Application作用域");

        return "responseData";
    }

    @RequestMapping(value = "/response_2")
    public ModelAndView response_2() throws Exception {
        /**
         * ModelAndView中的数据只保存在Request中。
         */
        ModelAndView mv = new ModelAndView();
        mv.addObject("mv_1", "ModelAndView中的数据1");
        mv.addObject("mv_2", "ModelAndView中的数据2");

        mv.setViewName("responseData");

        return mv;
    }

    /**
     * Model,Map,ModelMap中放入相同的数据,会前后覆盖。
     * 
     * 
     * ModelAndView与上述3个放入相同的数据,最后起作用是ModelAndView中的数据。
     */
    @RequestMapping(value = "/response_3")
    public ModelAndView response_3(Model model, Map map, ModelMap modelMap)
            throws Exception {
        System.out.println("response_3方法");

        ModelAndView mv = new ModelAndView();
        mv.addObject("mv_1", "ModelAndView中的数据1");
        mv.addObject("common", "ModelAndView中放入的");

        model.addAttribute("model_1", "Model中的数据");
        model.addAttribute("model_2", "Model中的数据2");

        map.put("map_1", "Map中的数据1");
        map.put("map_2", "Map中的数据2");

        modelMap.addAttribute("modelMap_1", "ModelMap中的数据");
        modelMap.put("modelMap_2", "ModelMap中的数据2");
        modelMap.addAttribute("common", "ModelMap中放入的");
        map.put("common", "Map中放入的");
        model.addAttribute("common", "Model中放入的");

        mv.setViewName("responseData");
        return mv;
        // return "responseData";
    }

}
  • ModelAndView中的数据只保存在Request中。
  •  Model,Map,ModelMap中放入相同的数据,会前后覆盖。
  •  ModelAndView与上述3个放入相同的数据,最后起作用是ModelAndView中的数据。

 

结果:

 

posted @ 2017-11-03 10:05  shyroke、  阅读(343)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~