×
☰ See All Chapters

Spring MVC Example with JSP

In this tutorial you will learn simple web application using the spring MVC framework. home.jsp is the request page from which request is sent to server. Request is sent to welcome.do, this URI is configured to DemoController. From DemoController we process the request and message is returned in ModelAndView object. ModelAndView object is created along with view name. ViewResolver will identify the physical location of view, later response data (model) is processed with the view and response is returned to user.

Mapping in spring mvc example

spring-mvc-example-0
 

Eclipse Project directory structure

spring-mvc-example-1
 

home.jsp

<html>

<head>

</head>

<body bgcolor="yellow">

Hello! <a href="welcome.do"><br> Click here to go to Welcome page</a>

</body>

</html>

 

welcome.jsp

<html>

<body bgcolor="pink">

     ${welcomeMessage}

</body>

</html>

 

DemoController.java

package com.java4coding;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

 

public class DemoController implements Controller {

        @Override

        public ModelAndView handleRequest(HttpServletRequest arg0,

                        HttpServletResponse arg1) throws Exception {

                String message = "Hurray! </br>My first Spring MVC Applications.";

 

                return new ModelAndView("welcome", "welcomeMessage", message);

        }

}

 

welcome-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xmlns:context="https://www.springframework.org/schema/context"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd

                https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-5.0.7.xsd">

 

        <bean name="/pages/welcome.do" class="com.java4coding.DemoController" />

       

        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

 

        <bean id="viewResolver"

                class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                <property name="prefix" value="/pages/" />

                <property name="suffix" value=".jsp" />

        </bean>

</beans>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/j2ee" xmlns:web="https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">

<servlet>

  <servlet-name>welcome</servlet-name>

     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

     <load-on-startup>1</load-on-startup>

  </servlet>

 

  <servlet-mapping>

     <servlet-name>welcome</servlet-name>

     <url-pattern>/</url-pattern>

  </servlet-mapping>

</web-app>

Output

spring-mvc-example-2
 

When “Click here to go to Welcome page”  link is clicked

spring-mvc-example-3
 

All Chapters
Author