DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using ZK With Spring Boot
  • Develop Web Application With Spring Boot in 30 minutes
  • How to Enable HTTPS on a Spring Boot Application
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Trending

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • A Guide to Container Runtimes
  • Docker Model Runner: Streamlining AI Deployment for Developers
  • Internal Developer Portals: Modern DevOps's Missing Piece
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot: Integrating Static Content

Spring Boot: Integrating Static Content

In this post, we go over the basics of creating static content for a web applicaiton by integrating several different web development tools, frameworks, and languages.

By 
Ranga Karanam user avatar
Ranga Karanam
DZone Core CORE ·
Jan. 17, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
59.6K Views

Join the DZone community and get the full member experience.

Join For Free

This guide will help you create a simple web application with Spring Boot. We will add static content (CSS and JS) and use it from a JSP view.

You Will Learn

  • How to Bootstrap a simple project with Spring Initializr.
  • How to initialize a basic web application for Spring Boot.
  • How to add a JSP for a web application.
  • How to add static content - JS and CSS.

Tools You Will Need

  • Maven 3.0+ is your build tool.
  • Your favorite IDE. We use Eclipse.
  • JDK 1.8+

Overview of the Web application

We will build a static todo page (unformatted) rendered using a JSP.

Files

The following screenshot shows an Eclipse project with all the files we will create.

Here's a brief overview of all our files:

  • SpringBootWebApplicationBootstrapJqueryApplication.java - Spring Boot Application class. This initializes the Spring Boot application with auto-configuration.
  • WelcomeController.java - A Controller with a method to redirect to the view - JSP.
  • welcome.jsp - The view - using our custom JS and CSS.
  • application.properties - This is typically used to configure frameworks in Spring Boot. In this example, we would configure our view resolver in application.properties.
  • custom.css - contains some basic CSS.
  • custom.js - contains a JavaScript alert.

You can find the complete project on GitHub.

Bootstrapping a Web Application With Spring Initializr

Creating a web application with Spring Initializr is a cake walk.

Spring Initializr is great tool to bootstrap your Spring Boot projects.

As shown in the image above, the following steps have to be performed: 

  • Launch Spring Initializr and choose the following:
    • Choose com.in28minutes.springboot.web.application as the Group.
    • Choose spring-boot-web-application-bootstrap-jquery as the Artifact.
    • Choose the following dependencies:
      • Web
      • Actuator
      • DevTools
  • Click 'Generate Project.'
  • Import the project into Eclipse.

Project Dependencies

Spring Boot Starter Web provides all the dependencies and the auto-configuration needed to develop web applications. It is the first dependency we will use.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

We want to use JSP as the view. The default embedded servlet container for Spring Boot Starter Web is Tomcat. To enable support for JSPs, we would need to add a dependency on tomcat-embed-jasper.

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

Configuring a View Resolver

We would have our JSPs in /WEB-INF/jsp/. We would need to configure the view resolver with the prefix and suffix.

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Controller

Let's add in a simple controller redirecting to the view.

/src/main/java/com/in28minutes/springboot/tutorial/basics/application/configuration/WelcomeController.java

@Controller
public class WelcomeController {    
    @RequestMapping("/welcome")
    public String loginMessage(){
        return "welcome";
    }
}

The URL to this controller method will be http://localhost:8080/welcome

Adding a view

Let's create a simple with a basic HTML structure. We will create a basic table which we will want to format a little later.

/src/main/webapp/WEB-INF/jsp/welcome.jsp

<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <div class="container">
        <table class="table table-striped">
            <caption>Your todos are</caption>
            <thead>
                <tr>
                    <th>Description</th>
                    <th>Target Date</th>
                    <th>Is it Done?</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                    <tr>
                        <td>Todo 1</td>
                        <td>10/12/2017</td>
                        <td>No</td>
                        <td><a class="btn btn-warning" href="/edit-todo">Edit Todo</a></td>
                        <td><a class="btn btn-warning" href="/delete-todo">Delete Todo</a></td>
                    </tr>
            </tbody>
        </table>
        <div>
            <a class="btn btn-default" href="/add-todo">Add a Todo</a>

        </div>
    </div>
</body>
</html>

Adding Static Content (CSS and JS)

The recommended folder for static content is /src/main/resources/static.

We will create the CSS as shown below. It will give a light blue background color to the body of the page.

/src/main/resources/static/css/custom.css

body {
    background-color: lightblue;
}

Next, we will create a custom JS file that will throw an alert.

/src/main/resources/static/js/custom.js

alert("I'm active");

Using JS and CSS in View

Referring to the JS file:

<script src="js/custom.js"></script>

Referring to jQuery: 

<link href="css/custom.css"
        rel="stylesheet">

The complete, updated view is shown below.

/src/main/webapp/WEB-INF/jsp/welcome.jsp

<html>
<head>
    <title>Welcome</title>
    <link href="css/custom.css"
        rel="stylesheet">
</head>
<body>
    <div class="container">
        <table class="table table-striped">
            <caption>Your todos are</caption>
            <thead>
                <tr>
                    <th>Description</th>
                    <th>Target Date</th>
                    <th>Is it Done?</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                    <tr>
                        <td>Todo 1</td>
                        <td>10/12/2017</td>
                        <td>No</td>
                        <td><a class="btn btn-warning" href="/edit-todo">Edit Todo</a></td>
                        <td><a class="btn btn-warning" href="/delete-todo">Delete Todo</a></td>
                    </tr>
            </tbody>
        </table>
        <div>
            <a class="btn btn-default" href="/add-todo">Add a Todo</a>

        </div>
        <script src="js/custom.js"></script>
    </div>
</body>
</html>

Complete Code Example

/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.in28minutes.springboot.web.application</groupId>
    <artifactId>spring-boot-web-application-bootstrap-jquery</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-web-application-bootstrap-jquery</name>
    <description>Spring Boot Tutorial - Adding Bootstrap and jQuery to Web Application</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

/src/main/java/com/in28minutes/springboot/tutorial/basics/application/configuration/SpringBootWebApplicationBootstrapJqueryApplication.java

package com.in28minutes.springboot.tutorial.basics.application.configuration;

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

@SpringBootApplication
public class SpringBootWebApplicationBootstrapJqueryApplication {

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

/src/main/java/com/in28minutes/springboot/tutorial/basics/application/configuration/WelcomeController.java

package com.in28minutes.springboot.tutorial.basics.application.configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WelcomeController {    
    @RequestMapping("/welcome")
    public String loginMessage(){
        return "welcome";
    }
}

/src/main/resources/application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework.web=INFO

/src/main/resources/static/css/custom.css

body {
    background-color: lightblue;
}

/src/main/resources/static/js/custom.js

alert("I'm active");

/src/main/webapp/WEB-INF/jsp/welcome.jsp

<html>
<head>
    <title>Welcome</title>
    <link href="css/custom.css"
        rel="stylesheet">
</head>
<body>
    <div class="container">
        <table class="table table-striped">
            <caption>Your todos are</caption>
            <thead>
                <tr>
                    <th>Description</th>
                    <th>Target Date</th>
                    <th>Is it Done?</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                    <tr>
                        <td>Todo 1</td>
                        <td>10/12/2017</td>
                        <td>No</td>
                        <td><a class="btn btn-warning" href="/edit-todo">Edit Todo</a></td>
                        <td><a class="btn btn-warning" href="/delete-todo">Delete Todo</a></td>
                    </tr>
            </tbody>
        </table>
        <div>
            <a class="btn btn-default" href="/add-todo">Add a Todo</a>
            
        </div>
        <script src="js/custom.js"></script>
    </div>
</body>
</html>

/src/test/java/com/in28minutes/springboot/tutorial/basics/application/configuration/SpringBootWebApplicationBootstrapJqueryApplicationTests.java

package com.in28minutes.springboot.tutorial.basics.application.configuration;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootWebApplicationBootstrapJqueryApplicationTests {

    @Test
    public void contextLoads() {
    }

}
Spring Framework Spring Boot Web application

Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using ZK With Spring Boot
  • Develop Web Application With Spring Boot in 30 minutes
  • How to Enable HTTPS on a Spring Boot Application
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: