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
1. Add dependencies to pom.xml
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.1.1.RELEASE</version>
- </dependency>
-
-
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>3.0-alpha-1</version>
- </dependency>
2. Create the request page
Let's create a simple jsp page containing a link.
index.jsp
- <html>
- <body>
- <a href="hello">Click here...</a>
- </body>
- </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
- package com.javatpoint;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class HelloController {
- @RequestMapping("/hello")
- public String redirect()
- {
- return "viewpage";
- }
- @RequestMapping("/helloagain")
- public String display()
- {
- return "final";
- }
- }
4. Provide the entry of controller in the 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>
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
- <?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">
-
-
- <context:component-scan base-package="com.javatpoint" />
-
-
- <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 the other view components
viewpage.jsp
- <html>
- <body>
- <a href="helloagain">JavaCodeGuruJi Tutorials</a>
- </body>
- </html>
final.jsp
- <html>
- <body>
- <p>Welcome to Spring MVC Tutorial</p>
- </body>
- </html>
Output:
No comments:
Post a Comment