【问题标题】:ModelAndView and ModelMap in Spring MVCSpring MVC 中的 ModelAndView 和 ModelMap
【发布时间】:2018-08-07 06:36:54
【问题描述】:
 @RequestMapping(value = "/testmap", method = RequestMethod.GET)
    public ModelAndView testmap(ModelAndView model) {
        ModelMap map=new ModelMap();
        String greetings = "Greetings, Spring MVC! testinggg";
        model.setViewName("welcome");
        map.addAttribute("message", greetings); 
        return model;
    }

我有

${message}

在welcome.jsp 上。但它不会打印问候语。 能告诉我原因吗?

【问题讨论】:

  • 为什么你认为它应该被打印? “印刷”到底是什么意思?
  • 另外,您的问题到底是什么?标题和问题不同!
  • 打印在哪里?请提供更多上下文,为什么要同时使用ModelModelAndView?您可以使用addObject(attributeName, attributeValue) 方法在 ModelAndView 本身中设置模型属性。
  • 我已经编辑了这个问题。我只需要它的共振。
  • 您尝试过上面评论中建议的方式吗?

标签: spring-mvc model-view-controller


【解决方案1】:

模型是一个接口。它定义了模型属性的持有者,主要用于向模型添加属性。它包含四个 addAttribute (Overloaded) 和一个 mergeAttributes 和一个 containsAttribute 方法。

例子:

 @GetMapping("/showViewPage")
public String passParametersWithModel(Model model) {
    Map<String, String> map = new HashMap<>();
    map.put("spring", "mvc");
    model.addAttribute("message", "Baeldung");
    model.mergeAttributes(map);
    return "viewPage";
}

ModelAndView 是一个类,它允许我们在一次返回中传递 Spring MVC(模型和视图)所需的所有信息。

例子:

@GetMapping("/goToViewPage")
public ModelAndView passParametersWithModelAndView() {
    ModelAndView modelAndView = new ModelAndView("viewPage");
    modelAndView.addObject("message", "Baeldung");
    return modelAndView;
}

希望您对此有所了解。

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2015-03-27
    • 2013-08-31
    • 2011-05-24
    • 1970-01-01
    相关资源
    最近更新 更多