Search code examples
springspring-mvcmodelattributespring-formmodelandview

Why ModelMap, Model, @ModelAttribute, ModelAndView required when a simple return of jsp page name in string can do the work


I have the following spring mvc code snippet that works fine:

Controller snippet:

@RequestMapping(value="/submitForm", method=RequestMethod.POST)
public String formSubmission(Employee employee)
{
    return "EmployeeWelcome";
}

On form data printing page:

<html>
<h4>List of Request Parameters</h4>
<h4>first name - ${employee.firstName}</h4>
<h4>last name - ${employee.lastName}</h4>
<h4>user email - ${employee.email}</h4>
<h4>user salary - ${employee.salary}</h4> 
</html>

The above prints all the values successfully, then why is the following required?

ModelMap, Model, @ModelAttribute, ModelAndView

Solution

  • If you want to get the model value or any message to the Ui from the java side then you would need these

    You can check the details here: What are the differences between Model, ModelMap, and ModelAndView?

    Check the docs: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/ModelAndView.html