Spring MVC Hello World Example:-
The following example show how to write a simple web based Hello
World application using Spring MVC framework. To start with it, let
us have working Eclipse IDE in place and follow the following steps to
develope a Dynamic Web Application using Spring Web Framework:
1 Create a Dynamic Web Project with a name SpringMVC and create a
package r4r.in under the src folder in the created project.
2 AddSpring and other libraries into the folder WebContent/WEB-INF/lib.
3 Create a Java class HelloWorldController under the r4r.in package.
4 Create Spring configuration files Web.xml and spring-servlet.xml under theWebContent/WEB-INF folder.
5 Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create a view file hello.jsp under this sub-folder.
6 The final step is to create the content of all the source and configuration files and export the application as explained below.
Following are the list of JAR files required for this application.
1 commons-logging-1.0.4.jar
2 jstl-1.2.jar
3 org.springframework.asm-3.0.1.RELEASE-A.jar
4 org.springframework.beans-3.0.1.RELEASE-A.jar
5 org.springframework.context-3.0.1.RELEASE-A.jar
6 org.springframework.core-3.0.1.RELEASE-A.jar
7 org.springframework.expression-3.0.1.RELEASE-A.jar
8 org.springframework.web.servlet-3.0.1.RELEASE-A.jar
9 org.springframework.web-3.0.1.RELEASE-A.jar
1 Now Here is the content of HelloWorldController.java file:
package r4r.in;
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 Spring MVC Example";
return new ModelAndView("hello", "message", message);
}}
2 Following is the content of Spring Web configuration 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"
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>SpringMVC</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>
3 Following is the content of another Spring Web configuration file 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="r4r.in" />
<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>
4 Following is the content of Spring view file hello.jsp.
<html>
<head>
<title>Spring MVC Series: Hello World - r4r.in</title>
</head>
<body>
${message}
</body>
</html>
5 Following is the content of Spring view file index.jsp.
<html>
<head>
<title>Spring MVC Series: Index - r4r.in</title>
</head>
<body>
<a href="hello.html">Click Here</a>
</body>
</html>
6 Output:
Hello Spring MVC Example