English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring MVC model interface

In Spring MVC, the model works on a container that contains application data. Here, data can take any form, such as objects, strings, information in databases, etc.

It is necessary to add Model Interface placed in the controller part of the computer. Application. HttpServletRequest Object reads the information provided by the user and passes it to Model Interface. Now, the view page can easily access the data in the model part.

Model interface methods

MethodDescription
Model addAllAttributes(Collection <?> arg)It adds all the attributes from the provided collection to this Map.
Model addAllAttributes(Map <String,?> arg)It adds all the attributes from the provided Map to this Map.
Model addAllAttribute(Object arg)It adds the provided attribute to this Map using the generated name.
Model addAllAttribute(String arg0, Object arg1)It binds the attribute with the provided name.
Map<String, Object> asMap()It returns the current model attribute set as a Map.
Model mergeAttributes(Map<String,?> arg)It adds all the attributes from the added Map to this Map, with existing objects with the same name taking priority.
boolean containsAttribute(String arg)It indicates whether this model contains the attribute with the given name

Spring MVC model example

We create a login page containing username and password. Here, we use specific values to verify the password.

1Add the dependency to pom.xml

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>servlet-api</artifactId>  
    <version>3.0-alpha-1</version>  
</dependency>

2Create a request page

Here, we create a login page to receive the user's name and password.

index.jsp

<html>
<body>
<form action="hello">
UserName: <input type="text" name="name"/> <br><br>
Password: <input type="text" name="pass"/> <br><br> 
<input type="submit" name="submit">
</form>
</body>
</html>

3Create a controller class

In the controller class:

HttpServletRequest Used to read the HTML form data provided by the user. ModelContains the request data and provides it for the page to view.

HelloController.java

package com.w3codebox;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
  @RequestMapping("/hello")
  public String display(HttpServletRequest req, Model m)
  {
    //read the provided form data
    String name=req.getParameter("name");
    String pass=req.getParameter("pass");
    if(pass.equals("admin"))
    {
      String msg="Hello "+ name;
      //add a message to the model
      m.addAttribute("message", msg);
      return "viewpage";
    }
    else
    {
      String msg="Sorry"+ name+". You entered an incorrect password";
      m.addAttribute("message", msg);
      return "errorpage";
    } 
  }
}

4Provide controller entries in web.xml file

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
   <servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>    
</servlet>  
<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

5Define Bean in xml file

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!-- Provide support for component scanning -->
  <context:component-scan base-package="com.w3codebox" />
  <!--Provide support for conversion, formatting and validation -->
  <mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/></property>
        <property name="suffix" value=".jsp"></property>        
     </bean>
</beans>

6、create other view components

To run this example, the following view components must be located in the WEB-INF/in the jsp directory.

viewpage.jsp

<html>
<body>
${message}
</body>
</html>

errorpage.jsp

<html>
<body>
${message}
<br><br>
<jsp:include page="/index.jsp></jsp:include>
</body>
</html>

Output: