SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)

2、代码

  • HelloController.java:
@Controller
public class HelloController {

    @GetMapping("/abc")
    public String hello(Model model){
        model.addAttribute("msg","你好");
        return "success";
    }
}
  • application.properties:
# 配置SpringMVC的视图解析
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
  • success.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>SUCCESS</h1>
    <h3>${msg}</h3>
</body>
</html>
  • hello.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>Hello JSP</h1>
    <a href="abc">abc</a>
</body>
</html>

pom.xml:

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--使用JSP引擎,内置tomcat没有此依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--<scope>provided</scope> 这个需要被注释掉-->
        </dependency>

        <!-- jsp标签库 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、配置

  • Project Structure:
    SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)

  • Server:
    SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)

  • Deployment:
    SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)

4、运行

直接启动Tomcat报404错误,需要通过spring-boot:run运行。
SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)
SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)
注意上图,启动的端口并不是我配置的8081,那位知道可以告诉下。

http://localhost:8080/hello.jsp
SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)
SpringBoot 2.x 使用外置 Servlet 容器(Tomcat)

相关文章:

  • 2021-08-28
  • 2021-08-13
  • 2021-11-10
  • 2022-02-23
  • 2021-12-28
  • 2021-08-03
  • 2021-04-07
猜你喜欢
  • 2021-10-23
  • 2021-12-03
  • 2021-08-01
  • 2022-12-23
  • 2022-01-22
  • 2021-05-26
相关资源
相似解决方案