JSP 与 Spring Boot

Gob*_*uiz 4 spring jsp spring-boot

我正在尝试在我的项目中使用jsp,但是很难。我将这两个依赖项放在 pom.xml 中

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

<!-- If you want to use JSTL -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后我将这两行放入 aplication.properties 中

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
Run Code Online (Sandbox Code Playgroud)

我创建了一个名为“index.jsp”的页面并将其放入

<h1>
   INDEX
</h1>

<p>
   Lorem, ipsum dolor sit amet consectetur adipisicing elit. Aspernatur nam, nisi, repudiandae illum 
   ab a aut maxime deleniti voluptas praesentium dicta delectus. Voluptatibus consequuntur 
   necessitatibus hic eum suscipit, sequi autem.
</p>
Run Code Online (Sandbox Code Playgroud)

还有我的控制器:

package com.example.curso.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping("/")
    public String index() {
        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试访问路线时出现错误 在此输入图像描述

gti*_*333 6

根据您的堆栈跟踪,看起来 Thymeleaf 已配置为渲染引擎/视图解析器。这可能是因为你spring-boot-starter-thymeleaf在你的类路径和 spring boot 自动配置它。

\n

在较新的版本中,Spring Boot 已删除 JSP 支持。因此,您需要手动配置一些东西才能使其工作。

\n

如果您想使用 jsp 而不是 thymeleaf 作为主要渲染引擎,请spring-boot-starter-thymeleaf从依赖项中删除 并将 .jsp 文件复制到 webapp 文件夹下,如下所示:

\n

应用程序属性:

\n
spring.mvc.view.prefix:/jsp/\nspring.mvc.view.suffix:.jsp\n
Run Code Online (Sandbox Code Playgroud)\n

文件夹结构:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pom.xml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 gt\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 SpringBootWebApplication.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 application.properties\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 webapp\n\xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 css\n\xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.css\n\xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 jsp\n\xe2\x94\x82\xc2\xa0\xc2\xa0             \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 welcome.jsp\n
Run Code Online (Sandbox Code Playgroud)\n

所需的依赖项:

\n
        <dependency>\n            <groupId>org.springframework.boot</groupId>\n            <artifactId>spring-boot-starter-web</artifactId>\n        </dependency>\n\n        <dependency>\n            <groupId>javax.servlet</groupId>\n            <artifactId>jstl</artifactId>\n        </dependency>\n\n        <dependency>\n            <groupId>org.apache.tomcat.embed</groupId>\n            <artifactId>tomcat-embed-jasper</artifactId>\n            <scope>provided</scope>\n        </dependency>\n
Run Code Online (Sandbox Code Playgroud)\n

欢迎.jsp

\n
<!DOCTYPE html>\n<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>\n<html lang="en">\n<head>\n    <c:url value="/css/main.css" var="jstlCss"/>\n    <link href="${jstlCss}" rel="stylesheet"/>\n</head>\n<body>\n    <h2>Message: ${msg}</h2>\n</body>\n\n</html>\n
Run Code Online (Sandbox Code Playgroud)\n

@控制器:

\n
    @RequestMapping("/")\n    String welcome(Model m) {\n        m.addAttribute("msg", "Hello");\n        return "welcome";\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

运行使用mvn spring-boot:run

\n

运行 @SpringBootApplication 的 main 方法可能不起作用

\n