Step-by-Step Guide to Building a Spring MVC HelloWorld Application
This tutorial walks through setting up a Spring MVC environment with Eclipse, Tomcat, and JDK, importing required JARs, configuring web.xml and springmvc.xml, writing a HelloWorld controller, creating JSP views, and handling common issues such as static resource mapping and server integration.
Preface: After a twelve‑year wait, the Chinese women’s volleyball team’s triumphant victory is likened to a programmer’s exhilaration when a stubborn bug is finally solved, inspiring the author to study Spring MVC in depth.
What is Spring MVC? Spring MVC is the web‑application module of the Spring Framework, providing a full‑featured Model‑View‑Controller architecture that can be used alone or integrated with other MVC frameworks such as Struts.
Software parameters used in this tutorial:
Eclipse Mars.1 Release (4.5.1)
Tomcat 8.0.36
JDK 1.8.0_60
Spring Framework 4.0.4.RELEASE
Creating the project: In Eclipse choose File → New → Other → Dynamic Web Project. The resulting directory structure is shown in the accompanying screenshots.
Importing required JARs: Place the following Spring and utility JARs into WEB-INF/lib:
spring-aop-4.0.4.RELEASE.jar
spring-beans-4.0.4.RELEASE.jar
spring-context-4.0.4.RELEASE.jar
spring-core-4.0.4.RELEASE.jar
spring-expression-4.0.4.RELEASE.jar
spring-web-4.0.4.RELEASE.jar
spring-webmvc-4.0.4.RELEASE.jar
commons-logging-1.1.1.jarConfiguration files and code:
web.xml (under WEB-INF)
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- Configure DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>springmvc.xml (under src)
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- Scan the controller package -->
<context:component-scan base-package="com.jackie.springmvc"/>
<!-- View resolver: prefix + view name + suffix -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>HelloWorld.java (package com.jackie.springmvc.handlers)
package com.jackie.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
/**
* 1. Map the request URL with @RequestMapping.
* 2. The returned string is resolved by the view resolver as prefix+returnVal+suffix.
*/
@RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world");
return "success";
}
}index.jsp (under WebContent)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld">hello world</a>
</body>
</html>success.jsp (under WEB-INF/views)
<%@ 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>
<h4>Success Page</h4>
</body>
</html>Additional notes:
1. Adding Tomcat server – Download Tomcat, add it in Eclipse via Window → Preferences → Server → Runtime Environments , then add the server runtime to the project’s build path (Project → Properties → Java Build Path → Libraries → Add Library → Server Runtime).
2. Serving static resources – Because the servlet mapping / intercepts all requests, static files (css, js, images, html) return 404. To allow them, add default servlet mappings in web.xml for each file type, e.g.:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
... (repeat for *.gif, *.jpg, *.js, *.html) ...After adding these mappings, static resources are correctly served, and the view resolver can also be configured to return other file types such as .jpg by changing the suffix property.
The article concludes with a brief promotion of additional Java project tutorials and contact information for the “Java group leader”.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
