springboot notes - using JSP

Use JSP in Spring Boot Project:

Project structure: webapp need to add the folder used to store files directory jsp

spring-boot-jsp
 +-src
    +- main
         +- java
         +- resources
         +- webapp
              +- WEB-INF
                 +- jsp
                    +- welcome.jsp
    +- test
 +-pom.xml

Jsp and suffix specified location in the configuration file in application.properties.
= spring.mvc.view.prefix / the WEB-INF / JSP /
spring.mvc.view.suffix = .jsp

Introducing dependencies

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

 

Simply write a page:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsp welcome</title>
</head>
<body>
    Time:  ${time}
    <br>
    Message: ${message}
</body>
</html>

 

Backend

@Controller
public class WelcomeController {

    @GetMapping("/")
    public String welcome(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", "hello world");
        return "welcome";
    }

}

Playing war run in the tomcat:
(1) set in the pom.xml in packaged format war.
<Packaging> WAR </ Packaging>
(2) embedded Tomcat dependent negative


Excluded embedded Tomcat depend, to avoid conflict jar package when packaging.

 

<dependency> 
    <the groupId> org.springframework.boot </ the groupId> 
    <the artifactId> Starter-spring-Boot-Web </ the artifactId> 
    <-! exclude built-in container, derived container excluded built into the outer container allows the war package running spring -boot project -> 
    <Exclusions> 
        <Exclusion> 
            <the groupId> org.springframework.boot </ the groupId> 
            <the artifactId> Starter-Spring-Boot-Tomcat </ the artifactId> 
        </ Exclusion> 
    </ Exclusions> 
</ dependency>

 

Servlet support:
Spring Boot project must implement the interface to configure SpringBootServletInitializer () method to make the outer container to run Spring Boot project, create ServletInitializer class under similar directory to start:

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JspApplication.class);
    }
}

 

Packaged and released, execute maven command in the root directory of the project:
mvn Clean Package Penalty for
the war package can be published to Tomcat.

Guess you like

Origin www.cnblogs.com/qq99514925/p/11570011.html