1、ModelMap的用法

package com.jt;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value="view")
public class TestModelAndView {
    @RequestMapping(value="otherPersonInfo")
    public String viewInfoOther(ModelMap map){
        map.addAttribute("name", "fy");
        map.addAttribute("gender","woman");
        return "viewPersonInfo";
    }
}

2、效果

 Spring MVC-从零开始-view-向页面传递data(ModelAndView与ModelMap的区别)

3、区别

  • ModelMap在参数中定义,由框架负责实例化,函数返回值

     

    为字符串,表示页面名称。ModelAndView需要手动定义并实例化,集数据与页面名称于一体
  • ModelMap不能重定向页面,ModelAndView可以设置重定向页面

 

相关文章:

  • 2022-12-23
  • 2021-12-30
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
猜你喜欢
  • 2021-08-25
  • 2022-12-23
  • 2021-09-06
  • 2021-07-19
  • 2022-12-23
  • 2021-06-07
相关资源
相似解决方案