EN
Spring Boot with JSP pages - simple app example
13 points
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
xxxxxxxxxx
1
2
<html>
3
<head>
4
<meta charset="utf-8" />
5
<title>Example template</title>
6
<link rel="stylesheet" href="css/style.css" />
7
<script type="text/javascript" src="js/script.js"></script>
8
</head>
9
<body>
10
11
<h1>Home page</h1>
12
<h1>
13
<span>Username from controller:</span>
14
15
<%
16
String username = (String) pageContext.findAttribute("username");
17
%>
18
<span><%= username %></span>
19
20
</h1>
21
22
</body>
23
</html>
application.properties
xxxxxxxxxx
1
server.port: 8080
2
spring.mvc.view.prefix: /WEB-INF/jsp/
3
spring.mvc.view.suffix: .jsp
4
logging.level.org.springframework.web=DEBUG
script.js
xxxxxxxxxx
1
console.log('test');
style.css
xxxxxxxxxx
1
body {
2
margin: 20px;
3
padding: 20px;
4
border: 1px solid grey;
5
border-radius: 4px;
6
}
SpringBootWebApplication.java
xxxxxxxxxx
1
package com.dirask;
2
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.boot.builder.SpringApplicationBuilder;
6
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7
8
9
public class SpringBootWebApplication extends SpringBootServletInitializer {
10
11
12
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
13
return application.sources(SpringBootWebApplication.class);
14
}
15
16
public static void main(String[] args) {
17
SpringApplication.run(SpringBootWebApplication.class, args);
18
}
19
}
MainController.java
xxxxxxxxxx
1
package com.dirask.controller;
2
3
import org.springframework.stereotype.Controller;
4
import org.springframework.web.bind.annotation.GetMapping;
5
6
import java.util.Map;
7
8
9
public class MainController {
10
11
// http://localhost:8080/
12
"/") (
13
public String getIndex(Map<String, Object> model) {
14
model.put("username", "Tom");
15
return "home";
16
}
17
}