In this example, we will learn how to write a Simple Web based Hello World application using Spring MVC framework using Spring 4 API.
Folder Structure :
- Create a Dynamic Web Project Spring4Tutorial and create a package for our src files “com.javainterviewpoint“
- Place the required jar files under WEB-INF/Lib
spring-aop-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-web-4.2.4.RELEASE.jar
spring-webmvc-4.2.4.RELEASE.jar
commons-logging-1.2.jar - Create the Java classes HelloWorldExample.java under com.javainterviewpoint folder.
- Place the index.jsp under “WebContent” directory.
- Create a folder “Jsp” under WEB-INF directory, for keeping our jsp files. Keep welcome.jsp under that folder
- Place the web.xml and SpringConfig-servlet.xml under the WEB-INF directory
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 4 Hello World Example - Spring MVC</title> </head> <body> <br> <div style="text-align:center"> <h3> <a href="welcoming.html">Click here to See the Welcome Message... </a> </h3> </div> </body> </html>
Our index page has nothing but a hyperlink which directs to our controller calling “welcome.html”
HelloWorldExample.java
package com.javainterviewpoint; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloWorldExample { @RequestMapping(value = "/welcoming", method = RequestMethod.GET) public ModelAndView dispWelcomeMessage() { String message = "Welcome to Spring 4 - Spring MCV Hello World Example !!.."; return new ModelAndView("welcome","message",message); } }
- We have annotated our “HelloWorldExample” class with @Controller annotation which tells Spring Container to treat this class as a Controller.
- @RequestMapping annotation on top of dispWelcomeMessage() redirects the request to this method, when the request given is “welcoming” and it can take only GET request which is denoted by method=RequestMethod.GET
- Finally return to the view page “welcome” along with our custom message passed to the ModelAndView class constructor
- Rendering the view will be taken care by “InternalResourceViewResolver” which is configured in our “SpringConfig-servlet.xml”
Welcome.jsp
Our Welcome page containts the EL for displaying the message which is passed from our controller
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h2>${message}</h2> </body> </html>
web.xml
The web.xml has everything about the application that a server needs to know, which is placed under the WEB-INF directory. <servlet-name> contains the name of the SpringConfiguration, when the DispatcherServlet is initialized the framework will try to load a configuration file “[servlet-name]-servlet.xml” under the WEB-INF directory.
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMVCFormHandling</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>SpringConfig</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SpringConfig</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
SpringConfig-servlet.xml
The SpringConfig-servlet.xml is also placed under the WEB-INF directory.
- <context:component-scan> will let the Spring Container to search for all the annotation under the package “com.javainteriviewpoint”.
- <mvc:annotation-driven/> annotation will activate the @Controller, @RequestMapping, @Valid etc annotations.
- The view is resolved through “org.springframework.web.servlet.view.InternalResourceViewResolver” which searches for the jsp files under the /WEB-INF/Jsp/ directory.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" 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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd" > <mvc:annotation-driven/> <context:component-scan base-package="com.javainterviewpoint"></context:component-scan> <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>
How Everything works?
- Upon running index.jsp and click on the “Click here to the Welcome Message” link.
- Once you click on the link, controll will be passed to the DispatcherServlet mentioned in the web.xml
<servlet-name>SpringConfig</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- Then the DispatcherServlet passes the controll to the HelloWorldExample.java(@Controller) .
- The action “Welcoming.html” be mapped in the controller, the controller verifies it under the @RequestMapping annotation.
- Now dispWelcomeMessage() method will be called, it simply returns the ModelAndView Object with the three parameter.
- 1st Parameter – View Name(welcome)
- 2nd Parameter -Key to access the message
- 3rd Parameter – Value
return new ModelAndView("welcome","message",message);
- Through the ViewResolver (InternalResourceViewResolver) mapped in the SpringConfig-servlet.xml , the welcome.jsp page will be resolved and rendered as the output.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/Jsp/"></property> <property name="suffix" value=".jsp"></property> </bean>
Output
URL : http://localhost:8080/Spring4Tutorial/index.jsp
Now Click on the HyperLink
Leave a Reply