Languages
[Edit]
EN

Spring Boot with JSP pages - simple app example

13 points
Created by:
Root-ssh
177650

Full and tested example of how to use Spring Boot + JSP pages.

Github repository with this example:

Download this example:

When we run SpringBootWebApplication.java under http://localhost:8080/ we will see:

Project structure:

home.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Example template</title>
	<link rel="stylesheet" href="css/style.css" />
	<script type="text/javascript" src="js/script.js"></script>
</head>
<body>

<h1>Home page</h1>
<h1>
    <span>Username from controller:</span>

    <%
        String username = (String) pageContext.findAttribute("username");
    %>
    <span><%= username %></span>

</h1>

</body>
</html>

application.properties

server.port: 8080
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
logging.level.org.springframework.web=DEBUG

script.js

console.log('test');

style.css

body {
    margin: 20px;
    padding: 20px;
    border: 1px solid grey;
    border-radius: 4px;
}

SpringBootWebApplication.java

package com.dirask;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWebApplication.class);
    }

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

MainController.java

package com.dirask.controller;

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

import java.util.Map;

@Controller
public class MainController {

    // http://localhost:8080/
    @GetMapping("/")
    public String getIndex(Map<String, Object> model) {
        model.put("username", "Tom");
        return "home";
    }
}

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Spring Boot

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join