SpringMVC sending data from controller to view and vice versa

1)Model 

Model will be place in the method parameter. We have to use model.addAttribute(String key, Object value)

@RequestMapping("/home")
public String home(Model model){
model.addAttribute("name", "Himal");
model.addAttribute("id", 2225);
return "home";
}

2)ModelAndView
With model and view, define it inside method. ModelAndView modelAndView = new ModelAndView("pagename");
return modelAndview
@RequestMapping("/help")
public ModelAndView help(){
ModelAndView modelAndView = new ModelAndView("help");
String s = "this is help";
List<String> stringList = new ArrayList<>();
stringList.add("Me");
stringList.add("Myself");
modelAndView.addObject("stringList", stringList);
modelAndView.addObject("s", s);
return modelAndView;
}

Comments

Popular posts from this blog