Spring Mvc Hello World Example
This Spring Mvc tutorial is for creating a simple Hello World program step by step.
The requirements for this tutorial are as follows:
1)Download JDK(Java Development Kit) here. Select the version based on your OS.
2)Download Eclipse from http://www.eclipse.org/downloads You can select Eclipse IDE for Java EE Developers 32/64 bits depending on your OS.
3)Download Spring jar files here. For this tutorial we will use 3.2.0.Release, also download commons-logging-1.1.1.jar file here.
Hello World Spring Mvc web project Steps
1) Create a web project in eclipse
Select File->New->Dynamic Web Project. We have named it SpringMvcHelloWorld. Select Tomcat as the run time environment.
2)Adding the jar files and create the project structure
We will add all the downloaded jar files into the lib folder of the WEB-INF directory. Below is the of the folder structure of the web project in eclipse.
3)Configure the Front Controller
Adding the <servlet-mapping> of DispatcherServlet in web.xml
We add the <servlet> block and the <servlet-mapping> block in the web.xml for the DispatcherServlet. By putting / in the <servlet-mapping> all the requests will go through the DispatcherServlet which acts like a FrontController in spring framework.
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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>SpringMvcHelloWorld</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |
4)Spring Configuration File
We will create a xml file named spring-mvc-servlet.xml
Based on the <servlet-name> entered in the web.xml for the DispatcherServlet we will create a xml file whose name will be <servlet-name>+”-servlet”.xml. So in our case it will be spring-mvc-servlet.xml. By default the DispatcherServlet looks for a xml file with “-servlet” appended to the name to load spring configurations. But it can also be configured explicitly from which xml file DispatcherServlet should load Spring Configurations.
Below entry will be required only if the name of the xml is not named according to <servlet-name>+”-servlet”.xml.
1 2 3 4 5 6 7 8 9 10 | <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/SpringMVCBeans.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> |
5)A HelloWorldController class
We will create a controller class named HelloWorldController in the package online.java.tutor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package online.java.tutor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping({"/","/hello"}) public String hello() { return "hello"; } @RequestMapping("/hi") public String hi() { return "hi"; } } |
@Controller annotation above the HelloWorldController class indicates that this is a controller class.This annotation is a specialization of the annotation @Component , which means <context: component-scan> will register @Controller-annotated classes as beans, as if they were annotated with @Component.
We need to configure a <context:component-scan> in spring-mvc-servlet.xml so that the HelloWorldController class (and all of the other controllers) will be automatically discovered and registered as beans.
<context:component-scan base-package=”online.java.tutor” />
The @RequestMapping annotation above the functions identify them as the request handling methods.
@RequestMapping({“/”,”/hello”}) above the hello() indicates that it will get executed with the default path contains “hello”.
@RequestMapping(“/hi”) indicates hi() will be executed when the path contains “hi”
The hello() function returns a String “hello” and hi() function returns “hi”. These returned String values are the logical name of the view that should render the results.
The DispatcherServlet will use these returned names to look up the actual view implementation by consulting a view resolver.
6)spring-mvc-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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="online.java.tutor" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
The InternalResourceView used above will dispatches the request to the JSP for rendering by prefixing /Web-INF/pages and appending .jsp to the logical name returned by the controller functions. So when the hello function returns “hello” the page that will render is /WEB-INF/pages/hello.jsp and similarly the hi().
7)Create view of the Mvc
We should create hello.jsp and hi.jsp in the pages folder in WEB-INF
hello.jsp
1 2 3 4 5 6 7 | <html> <head> /head> <body> <h1>Hello</h1> </body> </html> |
hi.jsp
1 2 3 4 5 6 7 | <html> <head> /head> <body> <h1>Hi</h1> </body> </html> |
8)Output
After deployment of the project to tomcat. When we do
http://localhost:8080/SpringMvcHelloWorld/hi on the browser we get
Hi
http://localhost:8080/SpringMvcHelloWorld/ or http://localhost:8080/SpringMvcHelloWorld/hello we get
Hello
You can download Source file of the Spring Mvc Hello World Example
SpringMvcHelloWorld
For private online java training please add me in skype.
Really a nice tutorial. Great post to see here. It would be really helpful for us to learn something from your blog post. Thanks for sharing it.
Thank you very much. It is very useful for me.