1. Overview

Spring boot web application development CSS and JS play an important role. This example contains Spring boot CSS, JS and Image Example. Data inside resources/static consider as resources so it will be accessible from jsp page directly.

2. Example


Spring boot CSS, JS and Image Example

Spring boot CSS, JS and Image Example

2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <description>spring boot js, css and image example</description>
    <!-- Inherit defaults from Spring Boot -->
        <!-- Provided -->
            <groupId>org.springframework.boot</groupId>                <!-- for tomcat web container-->
            <groupId>org.apache.tomcat.embed</groupId>              <!--fot jap compilation need provide scope runtime or provided because it available in tomcat -->
            <scope>runtime</scope>     <!-- in my case provided not working so write runtime-->
            <groupId>javax.servlet</groupId>           <!-- for jsp jstl tags-->
            <groupId>org.springframework.boot</groupId>         <!--its for spring mvc related -->

2.2 application.properties

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

2.3 SpringBootConfig

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

 * Created by JavaDeveloperZone on 19-07-2017.

// Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig extends SpringBootServletInitializer {
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootConfig.class);

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application

2.4 CSSJSController

package com.javadeveloperzone.controller;

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

 * Created by Lenovo on 19-07-2017.
public class CSSJSController {

    @GetMapping("/welcome")                     // it will handle all request for /welcome
    public String welcome() {
        return "welcome";           // welcome is view name. It will call welcome.jsp

2.4 custom.css

    color: red;

2.6 somescript.js

function myFunction() {
    document.getElementById("demo").innerHTML = "This content has been change by javascript";

2.7 welcome.jsp

    <title>spring boot js and css example</title>
    <link rel="stylesheet" href="/css/custom.css">
<h2>spring boot js and css example and color has been change using static/css/custom.css</h2>
<img src="/images/solr.png" height="50" width="50">

<div id="demo"></div>

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

2.8 Output:

Spring boot CSS, JS and Image Example - Output

Spring boot CSS, JS and Image Example – Output


3. References

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.

Leave a Reply

Your email address will not be published. Required fields are marked *