Eclipse Neon – Create Java Web Project with Spring – Without Maven

This post shows how to create a new Java web project with Spring framework support in Eclipse Neon. It is shown how the Spring 5 MVC (Model-View-Controller) can be used in a new dynamic web project in Eclipse Neon. This example does not use Maven for dependency management. The Spring 5 JARs are added to the project manually as a user library in Eclipse.

For an example on how to use Maven to create a Spring Web MVC project in Eclipse Neon from a Maven archetype, see this post:

Summary

  1. Download the Spring 5 Framework and JSTL Distributions
  2. Create a New Dynamic Web Project in Eclipse Neon
  3. Setup New User Libraries for Spring 5 and JSTL and Add to Web Project
  4. Add Spring 5 Framework Support to Dynamic Web Project
  5. Implement Simple Java Web Project with Spring Framework Support for MVC
  6. Run the Spring 5 Web Application on Tomcat 9 in Eclipe Neon

Related Posts

1  Download the Spring 5 Framework and JSTL Distributions

  • In this example, release 5.0.2 of the Spring framework is used. Download the distribution ZIP file by following the second link below.
  • Save the ZIP file spring-framework-5.0.2.RELEASE-dist.zip to some location on the disk and then unzip it. The contents are shown below. The lib folder contains all of the Spring 5 JAR files.
Spring 5 - List of Release 5.0.2 Distribution JAR Files
  • The below table lists all Spring 5 distribution JAR files included in the libs folder.
  • Other JARs located in that folder, not shown here, contain Java docs and source code.

The Spring 5 framework has a dependency on the JavaServer Pages Standard Tag Library (JSTL) and those JAR files need to be added to a project with Spring 5 support as well. For more information on JSTL, see the JSTL documentation from Oracle.

  • Download the 2 JSTL JAR files from the sources below and save them to your file system.
  • In sections 3 and 4 of this post, it is shown how to add the Spring 5 and JSTL JAR files to a dynamic web project so that it supports the Spring 5 framework.

2  Create a New Dynamic Web Project in Eclipse Neon

  • From the Eclipse Neon main menu, select File > New > Dynamic Web Project.
Eclipse Neon - Use Menu to Create New Dynamic Web Project
  • Enter the project name. Here it is SpringMVCProject.
  • Select the runtime environment. Here Apache Tomcat v9.0 is used.
  • Select the dynamic web module version. Here 3.1 is used.
Eclipse Neon - New Dynamic Web Project Form - Enter Name and Target Runtime
  • Click on Next to continue and then on Next again to accept the default Java build path configuration.
Eclipse Neon - New Dynamic Web Project Form - Configure Java Build Path
  • On the Web Module configuration step, enable Generate web.xml deployment descriptor.
Eclipse Neon - New Dynamic Web Project Form - Configure Web Module Settings
  • Click on Finish to complete the new dynamic web module setup.
  • Eclipse will open the project in the Project Explorer. The project structure is shown below.
Eclipse Neon - Project Explorer - View Web Project Structure
  • Continue with the next section to add Spring 5 framework support.

3  Setup New User Libraries for Spring 5 and JSTL and Add to Web Project

  • From the Eclipse Neon main menu, select Preferences and type "library" to filter the items.
  • Under Java > Build Path, select User Libraries and click on the New… button on the form.
Eclipse Neon - View Project Properties - User Libraries on Build Path
  • On the New User Library form, enter the library name, here Spring5, and click OK.
Eclipse Neon - New User Library - Enter Library Name
  • Select the Spring5 user library and click on the Add External JARs button.
Eclipse Neon - New User Library - Add External JAR Files
  • Select all 21 Spring 5 JAR files. Adding the -javadoc.jar and -sources.jar files is optional.
Eclipse Neon - New User Library - Select Spring 5 JAR Files from File System
  • The final Spring5 user library configuration should look like below. Click on OK to complete the setup.
Eclipse Neon - New User Library - View Spring 5 User Library JAR Files
  • Add another user library for JSTL, following the same steps, so that there are two libraries.
Eclipse Neon - Add User Library for JSTL Support
  • In the Project Explorer, right-click on the project and select Build Path > Configure Build Path.
Eclipse Neon - Project Explorer - Configure Build Path
  • Select Java Build Path in the left-hand side list and then click on Libraries in the form.
  • Proceed by clicking on the Add Library… button.
Eclipse Neon - Project Properties - Java Build Path Libraries
  • Select User Library for the library type and click on the Next… button.
Eclipse Neon - Project Properties - Select a Library Type to Add
  • Select the two user libraries that were configured in section 2, here named JSTL1.2 and Spring5.
Eclipse Neon - Project Properties - Select User Libraries to Add to Java Build Path
  • Click on Finish to complete the java build path changes.
  • Then click on OK to close the project properties dialog.
Eclipse Neon - Project Properties - View Configured User Libraries for Spring 5 and JSTL
  • The new user libraries are now shown in the project explorer under Java Resources > Libraries.
Eclipse Neon - Project Explorer - View Configured User Libraries under Java Resources Node

4  Add Spring 5 Framework Support to Dynamic Web Project

  • This section shows how to configure a Java web project with Spring framework support.
  • Modify the web.xml deployment descriptor to add the Spring DispatcherServlet and its mapping as shown below. Note the reference to the Spring contextConfigLocation in line 18.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>SpringMVCProject</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>webapp-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-web-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>webapp-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>
  • Create the spring-web-config.xml configuration file in the same location as the web.xml file.
  • In line 13, the package for Spring components such as controllers is set to com.pgx.java.web.
  • In line 15, the InternalResourceViewResolver is set to resolve views in /WEB-INF/view/jsp/
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd ">

  <context:component-scan base-package="com.pgx.java.web" />

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/view/jsp/" />
    <property name="suffix" value=".jsp" />
  </bean>

  <mvc:resources mapping="/resources/**" location="/resources/" />

  <mvc:annotation-driven />

</beans>
  • In Eclipse, the setup should look like the below screen, with the 2 XML files in WebContent/WEB-INF.
Eclipse Neon - Project Explorer - View Java Web Project with Spring Framework and web.xml and spring-web-config.xml Configuration Files
  • Note that, the below property of the InternalResourceViewResolver, configured in spring-web-config.xml results in the dependency of JSTL.
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

5  Implement Simple Java Web Project with Spring Framework Support for MVC

  • In the Eclipse Neon Project Explorer, right-click on the project name and select New > Class.
Eclipse Neon - New Java Class Form, Enter Package and Class Name
  • Enter the package and the class name. Make sure to use the base-package name that was set in the spring-web-config.xml for the component-scan context. Here it is com.pgx.java.web.
  • The below code shows the implementation of a Spring 5 controller using the @Controller annotation.
package com.pgx.java.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.pgx.java.bean.Person;

@Controller
public class MyController {

    @GetMapping("/Hello")
    public String helloView(Model model) {
        
        Person p = new Person();
        p.setTitle("Dr.");
        p.setFirstName("John");
        p.setLastName("Watson");
        
        model.addAttribute("person", p);
    
        return "HelloWorld";
    }
}
  • The Person bean is added to the Model so that it can be accessed with Expression Language (EL) in JSP files. The source code of the Person bean is shown below.
package com.pgx.java.bean;

public class Person {
  
  private String title;
  private String firstName;
  private String lastName;
  
  public String getTitle() {
    return title;
  }
  
  public void setTitle(String title) {
    this.title = title;
  }
  
  public String getFirstName() {
    return firstName;
  }
  
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  
  public String getLastName() {
    return lastName;
  }
  
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
  • In the Eclipse Neon Project Explorer, right-click on the project name and select New > JSP File.
Eclipse Neon - New JSP File Form, Enter Location and File Name
  • The view here is called HelloWorld.jsp and is located in WEB-INF/view/jsp.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Hello World!</title>
    </head>
    <body>
        Welcome ${person.title} ${person.firstName} ${person.lastName}!
    </body>
</html>
  • In line 9, EL is used to access the properties of the Person bean in the model.

6  Run the Spring 5 Web Application on Tomcat 9 in Eclipe Neon

  • In section 2, when the project was setup, Tomcat 9 was selected as the target runtime environment.
  • If needed, refer to the below post for adding a Tomcat 9 server in Eclipse Neon.
  • Before the Spring 5 MVC web application can be run on the Tomcat web server in Eclipse, the project’s Deployment Assembly must be configured. Use Project > Properties to open it.
  • Use the Add… button to include the 2 user libraries needed for Spring 5 as shown below.
Eclipse Neon - Project Properties - View Deployment Assembly for Java Web Project with Spring Framework Support
  • Click on OK to close the dialog and then right-click on the project and select Run As > Run on Server. Use the Console tab to view Tomcat server output and to look for error messages.
Eclipse Neon - View Running Tomcat 9 Server and Deployed Java Web Project with Spring Framework Support
  • In a web browser, access the view using the URL below.
http://localhost:8080/SpringMVCProject/Hello
  • The controller will populate the model and return the HTML output of the JSP to the browser.
Eclipse Neon - View HelloWorld Spring 5 JSP Output in Web Browser

To export the Java web project with Spring framework support as a deployable WAR file, see the post below. The WAR file can then be deployed to an external Tomcat instance or external JEE application server such as JBoss or Websphere.

6 thoughts on “Eclipse Neon – Create Java Web Project with Spring – Without Maven

  1. where is it specified that helloWorld.jsp is mapped to mapping “/Hello” & MyController??

    Can I move such @GetMapping(“/Hello”) mappings to some xml file?? How?

  2. Hello Abhijeet,

    yes, the annotation in the MyController class specifies the mapping of the “/Hello” resource path to be handled by that method. The method returns a view with “HelloWorld”, which will resolve to the JSP file “HelloWorld.jsp”. Instead of using annotations, you can specify these mappings in the web.xml file.

    For details on Servlet mappings in the web.xml file, see: https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html

Leave a Reply