Although Spring Boot does not support JSP by default, in line with the principle of learning first, in the case of multi-party search of information, a number of integration operations have been carried out.
1 Maven dependence
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! - JSP Rendering Engine - > JSP Rendering Engine, JSP Rendering Engine, JSP Rendering Engine, JSP Rendering Engine, JSP Rendering Engine, JSP Rendering Engine
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<! - Provided attribute needs to be turned on if external Tomcat deployment is in place - >
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2 Controller code
@Controller
public class JspOnSpringBootController {
// Read the configuration from application. yml, if the default value is Hello Jsp
@Value("${application.hello:Hello Jsp}")
private String hello = "Hello Jsp";
/**
* Default page <br/>
*@ RequestMapping ("/") is different from @RequestMapping
* If you don't write parameters, it will be the global default page. If you add 404 pages, you will automatically access the page.
* If the parameter "/" is added, it will only be considered as the root page.
* This method can be accessed through localhost:7070 or localhost:7070/index
*/
@RequestMapping(value = {"/","/index"})
public String index(Model model) {
// Returns the string directly, and the frame defaults to the spring. view. prefix directory (index splicing spring. view. suffix) page.
// This example is / WEB-INF/jsp/index.jsp
model.addAttribute("name", "Landy");
model.addAttribute("hello", hello);
return "index";
}
}
3. Application. properties configuration
# Service Port (Dev)
server.port=7070
spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp
application.hello = hello jsp on spring boot
4 Jsp page code
<html>
<head>
<title>JSP on Spring Boot</title>
</head>
<body>
<h1 style="color: red">${name}, ${hello}</h1>
</body>
</html>
5 operation
It is reasonable to say that the results of the page can be seen smoothly after the completion of the above, but if the main method of running Spring Application directly can not see the results, report 404.
After a lot of searching on the internet, we can see why the article has to start by spring-boot: run after integrating JSP to see the specific reasons.
The three modes can operate normally.
5.1 spring-boot: run boot
Use commandmaven clean spring-boot:run
Or use idea’s Maven plug-in to run.
Access address: http://localhost:7070/index
5.2 packed into war bags
-
Setting the packaging property of the pom. XML file should be war.
<groupId>org.landy</groupId> <artifactId>spring-boot-lesson-4</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
- Execute packing commands
maven clean package
- Execute the run war package command
Java-jar war package name
- Access address: http://localhost:7070/index
5.3 External Tomcat Deployment Method
-
Make sure < scope > provided </scope > is not commented.
The Spring Book embedded Servlet API runtime is provided by Tomcat and needs to be removed at runtime.
-
modify
SpringBootLesson4Application
Class, need inheritanceorg.springframework.boot.web.support.SpringBootServletInitializer
And then implement its configuration method, specifically return builder. sources (class name. class); (Note that this cannot be here, this is an instance of a class, and xxx. class is a template for the class).When deploying external containers, you can’t rely on the main method of Application to start, but on the web. XML-like way of servlet to start Spring’s application context. At this point, you need to inherit SpringBootServletInitializer and implement the configuration method.
- Pack:
maven clean package
- The webapp directory under Tomcat can be renamed.
- Access address: http://localhost:8080/jsp-in-spring-boot
All the above three ways can successfully access the following pages.
6 Question Notes
When deploying external tomcat, you must inheritSpringBootServletInitializer
Class, if there is no inheritance, 404 errors will be reported, as shown on the following page.
There are two examples of code in this article.
- For projects that do not inherit the SpringBootServletInitializer class, see address: https://github.com/landy8530/…
- For projects that inherit the SpringBootServletInitializer class, see address: https://github.com/landy8530/…