欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot继承外部tomcat并且可以访问jsp页面

程序员文章站 2022-06-01 22:49:23
...

(1)
SpringBoot继承外部tomcat并且可以访问jsp页面
(2)pom依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency><!-- 添加servlet依赖 -->
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency><!-- 添加jstl -->
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Need this to compile JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

(3)编写controller

@Controller
public class HelloController {
    @GetMapping("/aaa")
    public String hello(Model model){
        model.addAttribute("msg","hello world");
        return "success";
    }
}

(4)创建webapp文件夹并且导入xml文件
SpringBoot继承外部tomcat并且可以访问jsp页面
(5)配置yaml文件

spring:
  mvc:
    view:
      prefix: / #标识在webapp下的文件
      suffix: .jsp

(6)创建index.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <a href="aaa">aaaa</a>
    </body>
</html>

(7)在webapp下创建success.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h1>${msg}</h1>
    </body>
</html>

(8)运行结果
SpringBoot继承外部tomcat并且可以访问jsp页面
总结:
错误点:
(1)pom依赖导入不全
(2)在springboot中SpringBoot的启动器需要在controller的同一级文件夹
SpringBoot继承外部tomcat并且可以访问jsp页面
否则任何请求都不能请求成功!!!
SpringBoot继承外部tomcat并且可以访问jsp页面
SpringBoot继承外部tomcat并且可以访问jsp页面
这一步自己老是忘,谨记谨记!!!!,创建文件夹不规范,或者在添加扫描文件路径
SpringBoot继承外部tomcat并且可以访问jsp页面