Spring Model Interface

Model Interface

Model is an interface that defines a holder for model attributes. It is also used to transfer data between various parts of the Spring MVC application. The Model interface is available in the org.springframework.ui package.

The Model interface acts as a data container that contains the data of the MVC application. The stored data can of any form such as String, Object, etc.

Following are some methods of the Model interface:

Method Description
Model addAllAttributes(Collection<?> attributeValues) It copies all the attributes in the provided Collection into this Map.
Model addAllAttributes(Map<String,?> attributes) It copies all the attributes in the provided Map into this Map.
Model addAttribute( Object attributevalue) It adds the given attribute to this Map through a generated name.
Model addAttribute(String attributeName, Object attributeValue ) It wraps the given attributes with the given name.
Map<String, Object> asMap() It returns a current set of model attributes as a Map.
Boolean containsAttribute(String attributeName) It searches whether the model contains the attribute of the given name.
Model mergeAttributes(Map<String,?> attributes) It copies all the attributes in the given Map into this Map, with the existing objects of the same name.

Example of Model interface

Following are the steps to create an example of adding form data to the Model object:

  1. Create a web request page

In this step, we are going to create a request page named index.jsp, which contains the link to other view pages.

index.jsp

 <html>
 <body>
 <h2> Spring MVC Web application </h2>
 <a href = "home_page"> Home page | </a>
 <a href = "about_us"> About Us | </a>
 <a href = "general-form"> Query Form </a>
 </body>
 </html> 
  • Create the Controller class

In this step, we are going to create a controller class named MainController.java, which returns the JSP view pages. Here, we are using the HttpServletRequest interface that provides request information for HTTP servlets.

MainController.java

 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 MainController {
             @RequestMapping("/general-form")
             public String showForm() {
                         return "genform";
             }
             @RequestMapping("/processform")
             public String addDataModel(HttpServletRequest request, Model model) {
                         // read form data from the HTML form
                         String name = request.getParameter("StudentName");
                         // create the message for display
                         String result = "We welcome you.. " + name ;
                         // add mesage to model
                         model.addAttribute("message", result) ;
                         return "formdata" ;
             }
   } 
  • Add the entry of Controller into web.xml

In this step, we are going to add the entry of the Controller into a 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://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id = "WebApp_ID" version = "3.1">
             <display-name>spring-mvc-demo</display-name>
             <absolute-ordering />
             <!-- Spring MVC Configs -->
             <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
             <servlet>
                         <servlet-name>dispatcher</servlet-name>
                         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                         <init-param>
                                     <param-name>contextConfigLocation</param-name>
                                     <param-value>/WEB-INF/spring-servlet.xml</param-value>
                         </init-param>
                         <load-on-startup>1</load-on-startup>
             </servlet>
             <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
             <servlet-mapping>
                         <servlet-name>dispatcher</servlet-name>
                         <url-pattern>/</url-pattern>
             </servlet-mapping>
 </web-app> 
  • Define bean into another XML file.

In this step, we are going to declare the bean inside another XML file (spring-servlet.xml).

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">
             <!-- Step 3: Add support for component scanning -->
             <context:component-scan base-package = "com.app.SpringMVC3" />
             <!-- Step 4: Add support for conversion, formatting and validation support -->
             <mvc:annotation-driven/>
             <!-- Step 5: Define Spring MVC view resolver -->
             <bean
                         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                         <property name = "prefix" value = "/WEB-INF/view/" />
                         <property name = "suffix" value = ".jsp" />
             </bean>
 </beans> 
  • Create other JSP pages.

In this step, we are going to create the other JSP view pages.

genform.jsp

 <%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
     pageEncoding = "ISO-8859-1"%>
 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="ISO-8859-1">
 <title>Spring MVC Form</title>
 </head>
 <body>
 <form action = "processform" method = "get" >
  <input type = "text" name = "StudentName" placeholder = "Write your name.." />
  <br></br>
  <input type = "submit" value = "Submit"/>
 </form>
 </body>
 </html> 

formdata.jsp

 <%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
     pageEncoding = "ISO-8859-1"%>
 <!DOCTYPE html>
 <html>
 <head>
 <meta charset = "ISO-8859-1">
 <title>Insert title here</title>
 </head>
 <body>
 <h3>Spring MVC Application</h3>
 <br></br>
 STUDENT NAME: ${param.StudentName}
 <br></br>
 MESSAGE: ${message}
 </body>
 </html> 

Output

Following are the outputs of the application:

Spring Model Interface
Spring Model Interface 1
Spring Model Interface 2
Spring Model Interface 3



ADVERTISEMENT
ADVERTISEMENT