1. Create a new Springboot project and choose a war package;
Image
2. Tick the Web module, Devtools (used as a hot deployment, does not affect the operation)
Image
3. After the creation is complete, the project structure is as follows:
Image
Adding a JSP dependency to the 4.pom.xml file
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
5. Add the following in the Application.properties file:
#path, create a new folder WEB-INF under the webapp folder, build a folder jsp down
Spring.mvc.view.prefix=/WEB-INF/jsp/
# filename suffix, for example: index.jsp, placed in the jsp folder
Spring.mvc.view.suffix=.jsp
The. yml file is configured as follows:
Spring
Mvc:
View
Prefix:/web-inf/test/
Suffix:. jsp
6. Create a new index.jsp file with the path structure:
7.index.jsp page content is as follows:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Login</title>
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/signin.css">
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/bootstrap.min.css">
<style type="text/css">
Body {
Padding-top: 50px;
}
.starter-template {
Padding: 40px 15px;
Text-align: center;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Spring Boot+JSP</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="starter-template">
<h2>Sign in</h2>
<form class="form-signin" role="form" name="form" action="/login" method="post">
<div class="form-group">
<label for="username">username</label>
<input type="text" class="form-control" name="username" value="" placeholder="username"/>
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" class="form-control" name="password" placeholder="password"/>
</div>
<input type="submit" id="login" value="Login" class="btn btn-primary"/>
</form>
</div>
</div>
</body>
</html>
8. The introduction of CSS files, this does not affect the effect, just index.jsp page will not look good
Image.png
9. New Indexcontroller.java
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() { return "index";
}
}
10. Start the service, Access Localhost:8080/index to access index.jsp success
lisa_777
Links: Https://www.jianshu.com/p/25d3ecbdd5e8
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.
Springboot Accessing JSP pages