In spring when control return to view i have seen it return through,
return new ModelAndView("contact", "command", new Contact());
But i have seen a example where just a string name returns as below,
@RequestMapping("/index")
public String listContacts(Map<String, Object> map) {
map.put("contact", new Contact());
map.put("contactList", contactService.listContact());
return "contact";
}
Is this also valid or a good practice?
Yes it is valid to return a String. It will be interpreted as the View name (ref):
Is it a good practice ?
Let’s say that’s a matter of taste. (My point of view is that this style requires more knowledge about implicit behavior of Spring MVC and so I don’t like it because it make the code more difficult to understand for someone not aware of all this implicit behavior, but it’s really just personal preference…)