Sunday, September 15, 2019

Spring MVC Multiple View page Example

Here, we redirect a view page to another view page.
Let's see the simple example of a Spring Web MVC framework. The steps are as follows:
  • Load the spring jar files or add dependencies in the case of Maven
  • Create the controller class
  • Provide the entry of controller in the web.xml file
  • Define the bean in the separate XML file
  • Create the other view components
  • Start the server and deploy the project

Directory Structure of Spring MVC

Spring MVC Multiple View page

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

Let's create a simple jsp page containing a link.

index.jsp
  1. <html>  
  2. <body>  
  3. <a href="hello">Click here...</a>  
  4. </body>  
  5. </html>  

3. Create the controller class

Let's create a controller class that returns the JSP pages. Here, we pass the specific name with a @Requestmapping annotation to map the class.
HelloController.java
  1. package com.javatpoint;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. @Controller  
  5. public class HelloController {  
  6. @RequestMapping("/hello")  
  7.     public String redirect()  
  8.     {  
  9.         return "viewpage";  
  10.     }     
  11. @RequestMapping("/helloagain")  
  12. public String display()  
  13. {  
  14.     return "final";  
  15. }  
  16. }  

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

Now, we also provide view resolver with view component.
Here, the InternalResourceViewResolver class is used for the ViewResolver.
The prefix+string returned by controller+suffix page will be invoked for the view component.
This xml file should be located inside the WEB-INF directory.
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. <!-- Define Spring MVC view resolver -->  
  20. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.         <property name="prefix" value="/WEB-INF/jsp/"></property>  
  22.         <property name="suffix" value=".jsp"></property>          
  23.      </bean>  
  24. </beans>  

6. Create the other view components

viewpage.jsp
  1. <html>  
  2. <body>  
  3. <a href="helloagain">JavaCodeGuruJi Tutorials</a>  
  4. </body>  
  5. </html>  
final.jsp
  1. <html>  
  2. <body>  
  3. <p>Welcome to Spring MVC Tutorial</p>  
  4. </body>  
  5. </html>  
Output:
Spring MVC Multiple View page

Spring MVC Multiple View page

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...