Before we starts with our first Hello World Spring MVC Example, we will need few tools.
After selecting Dynamic Web Project, press Next.
net.viralpatel.spring3.controller
in the source folder. This package will contain the Controller file. package net.viralpatel.spring3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
Code language: Java (java)
Note that we have annotated the HelloWorldController class with @Controller
and @RequestMapping("/hello")
on line 7 and 10. When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. The @RequestMapping
annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. That includes /hello/*
and /hello.html
. The helloWorld() method returns ModelAndView
object. The ModelAndView object tries to resolve to a view named “hello” and the data model is being passed back to the browser so we can access the data within the JSP. The logical view name will resolve to "/WEB-INF/jsp/hello.jsp"
. We will discuss this shortly how the logical name “hello” which is return in ModelAndView object is mapped to path /WEB-INF/jsp/hello.jsp. The ModelAndView object also contains a message with key “message” and value “Hello World, Spring 3.0!”. This is the data that we are passing to our view. Normally this will be a value object in form of java bean that will contain the data to be displayed on our view. Here we are simply passing a string. hello.jsp
under WEB-INF/jsp directory and copy following content into it. File: WEB-INF/jsp/hello.jsp <html>
<head>
<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
${message}
</body>
</html>
Code language: HTML, XML (xml)
The above JSP simply display a message using expression ${message}
. Note that the name “message” is the one which we have set in ModelAndView
object with the message string. Also we will need an index.jsp file which will be the entry point of our application. Create a file index.jsp under WebContent folder in your project and copy following content into it. File: WebContent/index.jsp <html>
<head>
<title>Spring 3.0 MVC Series: Index - ViralPatel.net</title>
</head>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
Code language: HTML, XML (xml)
org.springframework.web.servlet.DispatcherServlet
class in web.xml. Open web.xml file which is under WEB-INF folder and copy paste following code. File: WEB-INF/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"
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>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>*.html</url-pattern>
</servlet-mapping>
</web-app>
Code language: HTML, XML (xml)
The above code in web.xml will map DispatcherServlet with url pattern *.html. Also note that we have define index.jsp as welcome file. One thing to note here is the name of servlet in <servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml
in WEB-INF folder of web application. In this example, the framework will look for file called 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="net.viralpatel.spring3.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Code language: HTML, XML (xml)
In the above xml configuration file, we have defined a tag <context:component-scan>. This will allow Spring to load all the components from package net.viralpatel.spring3.controller
and all its child packages. This will load our HelloWorldController
class. Also we have defined a bean viewResolver
. This bean will resolve the view and add prefix string /WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. Note that in our HelloWorldController class, we have return a ModelAndView object with view name “hello”. This will be resolved to path /WEB-INF/jsp/hello.jsp. @Controller
and @RequestMapping
. In next article we will see how easy it is to handle form data using Spring 3.0 MVC. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Hi Viral
thanks for the post. I am getting the output as it is in the jsp,
like ${message}
can you tell what mistake i have done.
Spring 3.0 MVC Series -- good article to start learning Spring 3.0 MVC. Thanks.
HTTP Status 404 - /Spring3MVC/hello
--------------------------------------------------------------------------------
type Status report
message /Spring3MVC/hello
description The requested resource (/Spring3MVC/hello) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.26
spring is good but shine is realy better and faster
i prefer to continue working with shine Enterprise Pattern
it contains reflection too
http://sourceforge.net/projects/shine-enterpris/files/
If output is ${message} You need to create a bean in hello.jsp
And access it using
There is probably a more elegant solution but I'm new to this too
Edit: That didn't really work properly
If output is ${message} You need to create a bean in hello.jsp
jsp:useBean id="message" type="java.lang.String" scope="request" (inside angled brackets)
And access it using
%= message % (inside angled brackets)
There is probably a more elegant solution but I'm new to this too
If you wish that ${message} output, configure the page hello.jsp with:
The JSP ignore Expression Language implicitly and you need configure in JSP or web.xml
Sorry, the correct comment is:
If you wish that ${message} output, configure the page hello.jsp with:
%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%
The JSP ignore Expression Language implicitly and you need configure in JSP or web.xml
Correct comment:
If you wish that ${message} output, configure the page hello.jsp with:
% page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %
The JSP ignore Expression Language implicitly and you need configure in JSP or web.xml
Through tutorial but I have a problem with the result - the link in the index.jsp page is hello.html instead of hello.jsp. I think that's because it specifies a href="hello.html" but when I try to change it nothing changes. Cleaned the project, cleaned Tomcat, restarted, etc.
Good article though. So much chatter about Spring on teh internets and so few getting-started guides like this one. Thanks.