Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Nutrition books in the world. There is no book in life, there is no sunlight; wisdom without books, as if the birds do not have wings.
2018-05-12 16:35:27

Copy this link when reproducing:

To learn the Spring MVC framework, I'm going to create a web application named "Today in History", which has only one web page and it will display what happened today in history, such as historical facts, events, famous birthdays, world history, etc.

As usual, let's take it easy and enjoy some beautiful photos first.






Let's start.


Setup the Development Environment for Spring MVC

1. Make sure JDK, Tomcat server, and Eclipse have been installed to the computer and configured properly. Please see for detail steps.

2. Download Spring framework.

    I. Open a web browser and navigate to


    II. Scroll to the "Quick Start" section, you will find the recommended way to download the spring framework.

    III. Use maven project and the following dependencies to download Spring framework and other related libraries:


FYI: Check the link for more information about using Maven project to download libraries.

3. Create the "Today in History" project.

    I. Select "File -> New -> Project..." from the main menu.


    II. Select "Web -> Dynamic Web Project", and click "Next >" to continue.


    III: Type in the Project name and click "Next >" to continue.


    IV: Click "Next >" to continue.


    V. Tick "Generate web.xml deployment descriptor" and click "Finish", a new project will be created.

    VI. Right click on the project, select "Properties" on the popped menu.


    VII. Switch to "Java Build Path" tab, change the "Default output folder" to the ".../WEB-INF/classes" directory, and click "Apply and Close" to save the settings.

4. Copy the following .jar files into the "WebContent\WEB-INF\lib" folder:


5. Right click on the project, select "Refresh" from the popped menu to refresh the project.

6. Input the following lines into "web.xml" to indicate that:
    I.  we want to use the built-in servlet "org.springframework.web.servlet.DispatcherServlet".
    II. The configuration file for the DispatcherServlet is located in "WEB-INF/config" folder and named "springmvc-config.xml".
    III. All the requests will be mapped to this servlet. (<url-pattern>/</url-pattern>)


7. Create a folder named "config" in the "WEB-INF" directory, and put into a file named "springmvc-config.xml".

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns=""
     xsi:schemaLocation="     ">
 	<!-- Indicate to scan all controllers in the base-package -->
     <context:component-scan base-package=""/>
 	<!-- Indicate these are static resources -->
     <mvc:resources mapping="/css/**" location="/css/"/>
     <mvc:resources mapping="/*.html" location="/"/>
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 	    <!-- With prefix and suffix, we can refer to the view name itself -->
         <property name="prefix" value="/WEB-INF/jsp/"/>
         <property name="suffix" value=".jsp"/>


8. Right click on the "Src" item, select "New -> Package" from the popped menu.


9. Type in the package name and click "Finish" to create the package.

    Note: the name must be the same to the one in the "springmvc-config.xml".

10. Following the step No. 8 and No. 9, create another packages:

11. Create a class named "Day" in the package "", and put the following source code in:

 public class Day implements Serializable {
 	private static final long serialVersionUID = 1978924851058396786L;
 	private int id;
 	private String key;
 	private String name;
 	private String description;
 	public Day() {
 	public int getId() {
 		return id;
 	public void setId(int id) { = id;
 	public String getKey() {
 		return key;
 	public void setKey(String key) {
 		this.key = key;
 	public String getName() {
 		return name;
 	public void setName(String name) { = name;
 	public String getDescription() {
 		return description;
 	public void setDescription(String description) {
 		this.description = description;

12.Create an interface named "DayService" in the package "", and put the following source code in:

 public interface DayService {
 	Day GetByKey(String aKey);

13. Create another class named "DayServiceImpl" in the package "", and put the following source code in:

 import org.springframework.stereotype.Service;
 public class DayServiceImpl implements DayService {
 	public Day GetByKey(String aKey) {
         Day day = new Day();
         day.setDescription("A good day!");
 		return day;

14. Put the following text into the "springmvc-config.xml":

<context:component-scan base-package=""/>

15. Create a class named "DayController" in the package "", and put the following source code in:

 import java.text.SimpleDateFormat;
 import java.util.Date;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.RequestMapping;
 public class DayController {
     private DayService dayService;
 	@RequestMapping(value = "/")
 	public String Index(Model model) {
 	    Date today = new Date();
 	    SimpleDateFormat format = new SimpleDateFormat("MMdd");
 	    String key = format.format(today);
 	    Day day = dayService.GetByKey(key);
 	    return "DayForm";

16. Create a folder named "jsp" in the "WebContent/WEB-INF" folder, and create a .jsp file named "DayForm.jsp" in the "jsp" folder which was just created. Here is the content of "DayForm.jsp":

<%@ taglib prefix="form" uri="" %>
 <%@ taglib uri="" prefix="c" %>
 <title>Today in History</title>

17. Oops! Encountered an error in this jsp file: "Can not find the tag library descriptor for """.

18. Fix the error.

    I. Following the instruction in the step No. 1, use maven to download the "jstl" library. Here is the dependency you need to put into "pom.xml":


    II. Copy the downloaded library file "jstl-1.2.jar" into the "WebContent/WEB-INF/lib" folder, and refresh the project.

    III. It works! The error message disappeared.

19. Right click on the project, select "Run as -> Run on Server" from the popped menu.


20. Done! It works!