简体   繁体   English

如何在 Spring MVC 中使用内容类型 ModelAndView

[英]How use content type ModelAndView in Spring MVC

When Using this code使用此代码时

@RequestMapping("/index")
public ModelAndView showMessage(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    System.out.println("From controller..");
    ModelAndView mv = new ModelAndView("index");
    mv.addObject("hello", "Hello");
    mv.addObject("name", name);
    return mv;
}

View page查看页面

 <body> 
  <p>${hello} ${name}</p>
</body>

The "From controller" message in the server result but when print variable "hello" and "name" then it does not show in the view JSP page服务器结果中的“来自控制器”消息,但是当打印变量“hello”和“name”时,它不会显示在视图 JSP 页面中

replace代替

ModelAndView mv = new ModelAndView();
mv.setViewName("index");

you can also use:您还可以使用:

public String showMessage(Model model)
model.addAttribute("hello", "Hello");
return "index";

Just add this to your jsp <%@ page isELIgnored = "false" %> Right now your jsp might be ignoring the EL language that is ${''} Furthermore You are setting the view name in只需将其添加到您的 jsp <%@ page isELIgnored = "false" %>现在您的 jsp 可能会忽略${''}的 EL 语言此外您正在设置视图名称

ModelandView modelandview= new ModelandView("index");

so there is no need for this所以没有必要这样做

modelandview.setviewName("index");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM