A simple Spring website

Two pages: Home and About Us. Both contain fixed HTML.

1. Open the project

Extract the ZIP and open the hello-spring-simple folder. The setup files are already included. You need JDK 21+ and Maven 3.9+ installed.

2. Look at just these two files

Home: src/main/resources/static/index.html

<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><title>Home</title></head>
<body>
  <h1>Hello World</h1>
  <a href="/aboutus.html">About Us</a>
</body>
</html>

About Us: src/main/resources/static/aboutus.html

<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><title>About Us</title></head>
<body>
  <h1>About Us</h1>
  <p>This is our first Spring website.</p>
  <a href="/">Home</a>
</body>
</html>

The folder name static tells Spring Boot where to find these files. The special filename index.html becomes the Home page.

3. Choose a port

Open src/main/resources/application.properties. It contains server.port=8081. If 8081 is busy, change it to server.port=8082, save and restart the application. Then open http://localhost:8082/ and http://localhost:8082/aboutus.html.

4. Start the website

Open a terminal in hello-spring-simple, where pom.xml is located. Run:

mvn spring-boot:run

Wait until the terminal says the application has started. Keep that terminal open. Maven needs internet access for the first dependency download.

5. Open the pages

Home: http://localhost:8081/
About Us: http://localhost:8081/aboutus.html

Click About Us, then Home. There is no text box or form.

6. Make one change

Change Hello World in index.html to your own heading. Stop the application with Ctrl+C, run the same command again, then refresh the browser.

What happens?

Browser asks for /
  → Spring Boot serves static/index.html
  → Browser displays Hello World

Browser asks for /aboutus.html
  → Spring Boot serves static/aboutus.html
  → Browser displays About Us

Spring Boot serves these fixed HTML pages directly.

Open only when ready: the setup files

These files are already provided. You can run and edit the website before studying them.

HelloApplication.java

Located at src/main/java/com/edupoly/hello/HelloApplication.java. Its main method starts the application.

package com.edupoly.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

application.properties

Located at src/main/resources/application.properties. It chooses port 8081.

server.port=8081

pom.xml

Maven reads this file to download the web libraries and run the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>4.1.1</version>
    <relativePath/>
  </parent>
  <groupId>com.edupoly</groupId>
  <artifactId>hello-spring-simple</artifactId>
  <version>1.0.0</version>
  <properties>
    <java.version>21</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webmvc</artifactId>
    </dependency>
</dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

If it does not start

Check java -version and mvn -version. Maven must report Java 21 or newer. Run the start command from the folder containing pom.xml. Only one application can use port 8081 at a time; stop the earlier instance before starting another.

After Stage 1: the Spring words to recognize

Focus: Start the server and serve a fixed HTML file.

TermOur concrete exampleIts place in Spring
Spring BootHelloApplication starts our website.Boot sets up and starts the Spring application and embedded server.
Static resourcestatic/index.html and static/aboutus.html are served as files.Spring Boot provides a standard location for these files.
Configurationserver.port in application.properties chooses our port.Spring Boot reads settings that control application startup.
Maven dependencypom.xml includes the web starter.Maven brings the Spring web libraries into this project.

You should now be able to say: Spring Boot starts our server; Spring serves our HTML files from static.

One small check: Which file would you edit to change the port, and which file would you edit to change the Home heading?

When ready: useful next stages after Stage 7

A useful continuation: planned next stages

These are the next learning steps, not additional projects already implemented in this bundle. Each proposed change gives the new terminology a concrete example.

Next stageOne useful changeWords to learn when we make that change
8. A separate product-file readerMove the existing file-reading code into ProductFileReader and have Spring supply that object to ProductController through its constructor. Print object creation and request messages to observe reuse.@Component, dependency, constructor injection, DI, IoC and default singleton scope.
9. Product operationsAdd ProductService for rules such as choosing products currently in stock.@Service, business rule and separation of responsibilities.
10. Product data accessPlace file-based reading and lookup behind a product repository.Repository pattern, @Repository and data access.
11. MongoDB storageReplace the text-file storage with MongoDB while keeping the page flow familiar.@Document, ID, MongoRepository, persistence and CRUD.
12. Explicit bean configurationSupply a helper through a configured factory method and inspect how it is registered.@Configuration, @Bean, factory method, bean definition and registry.

Until Stage 8 is implemented, our examples do not demonstrate Spring injecting a separate ProductFileReader into ProductController. The current product controller still reads the file itself. Keep the original getInputStream() and try-with-resources code for that step.