Issue
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
Solution
replace
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
you can also use:
public String showMessage(Model model)
model.addAttribute("hello", "Hello");
return "index";
Answered By - SongChay Beng
Answer Checked By - Timothy Miller (JavaFixing Admin)