Sunday, September 15, 2019

Spring MVC Model Interface

In Spring MVC, the model works a container that contains the data of the application. Here, a data can be in any form such as objects, strings, information from the database, etc.
It is required to place the Model interface in the controller part of the application. The object of HttpServletRequest reads the information provided by the user and pass it to the Model interface. Now, a view page easily accesses the data from the model part.

Methods of Model Interface

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

Spring MVC Model Example

Let's create a login page that contains a username and password. Here, we validate the password with a specific value.

1. Add dependencies to pom.xml

  1.     <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-webmvc</artifactId>  
  5.     <version>5.1.1.RELEASE</version>  
  6. </dependency>  
  7.   
  8. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  9. <dependency>    
  10.     <groupId>javax.servlet</groupId>    
  11.     <artifactId>servlet-api</artifactId>    
  12.     <version>3.0-alpha-1</version>    
  13. </dependency>  

2. Create the request page

Here, we create the login page to receive name and password from the user.
index.jsp
  1. <html>  
  2. <body>  
  3. <form action="hello">  
  4. UserName : <input type="text" name="name"/> <br><br>  
  5. Password : <input type="text" name="pass"/> <br><br>   
  6. <input type="submit" name="submit">  
  7. </form>  
  8. </body>  
  9. </html>  

3. Create the controller class

In controller class:
  • The HttpServletRequest is used to read the HTML form data provided by the user.
  • The Model contains the request data and provides it to view page.
HelloController.java
  1. package com.javatpoint;  
  2. import javax.servlet.http.HttpServletRequest;  
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.ui.Model;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. @Controller  
  8. public class HelloController {  
  9.   
  10.     @RequestMapping("/hello")  
  11.     public String display(HttpServletRequest req,Model m)  
  12.     {  
  13.         //read the provided form data  
  14.         String name=req.getParameter("name");  
  15.         String pass=req.getParameter("pass");  
  16.         if(pass.equals("admin"))  
  17.         {  
  18.             String msg="Hello "+ name;  
  19.             //add a message to the model  
  20.             m.addAttribute("message", msg);  
  21.             return "viewpage";  
  22.         }  
  23.         else  
  24.         {  
  25.             String msg="Sorry "+ name+". You entered an incorrect password";  
  26.             m.addAttribute("message", msg);  
  27.             return "errorpage";  
  28.         }     
  29.     }  
  30. }  

4. Provide the entry of controller in the web.xml file

web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>SpringMVC</display-name>  
  4.    <servlet>    
  5.     <servlet-name>spring</servlet-name>    
  6.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  7.     <load-on-startup>1</load-on-startup>      
  8. </servlet>    
  9. <servlet-mapping>    
  10.     <servlet-name>spring</servlet-name>    
  11.     <url-pattern>/</url-pattern>    
  12. </servlet-mapping>    
  13. </web-app>  

5. Define the bean in the xml file

spring-servlet.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13.   
  14.     <!-- Provide support for component scanning -->  
  15.     <context:component-scan base-package="com.javatpoint" />  
  16.   
  17.     <!--Provide support for conversion, formatting and validation -->  
  18.     <mvc:annotation-driven/>  
  19. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  20.         <property name="prefix" value="/WEB-INF/jsp/"></property>  
  21.         <property name="suffix" value=".jsp"></property>          
  22.      </bean>  
  23. </beans>  

6. Create the other view components

To run this example, the following view components must be located inside the WEB-INF/jsp directory.
viewpage.jsp
  1. <html>  
  2. <body>  
  3. ${message}  
  4. </body>  
  5. </html>  
errorpage.jsp
  1. <html>  
  2. <body>  
  3. ${message}  
  4. <br><br>  
  5. <jsp:include page="/index.jsp"></jsp:include>  
  6. </body>  
  7. </html>  
Output:

Spring MVC Model Interface

Spring MVC Model Interface


Spring MVC Model Interface

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...