带有Spring Boot的JSP [英] JSP with Spring Boot

查看:72
本文介绍了带有Spring Boot的JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use jsp in my project, but it is difficult. I put these two dependencies in the 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>

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

Then I put these two lines in the aplication.properties

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

我创建了一个名为"index.jsp"的页面,然后放上

I created a page with the name "index.jsp" and put that

<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>

还有我的控制器:

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";
    }
}

当我尝试访问路由时出现错误

And the error when I try to access the route

推荐答案

基于您的堆栈跟踪,Thymeleaf被配置为呈现引擎/视图解析器.可能是因为您的类路径中有 spring-boot-starter-thymeleaf ,并且spring boot自动对其进行了配置.

Based on your stacktrace, it looks Thymeleaf is configured as rendering engine/view resolver. It might be because you have spring-boot-starter-thymeleaf in your classpath and spring boot auto configured it.

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

JSP support has been dropped from Spring Boot in newer versions. So, you will need manually configure few things to make it work.

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

If you want to use jsp instead of thymeleaf as your primary rendering engine, remove the spring-boot-starter-thymeleaf from your dependency and copy your .jsp files under webapp folder as below:

application.properties:

application.properties:

spring.mvc.view.prefix:/jsp/
spring.mvc.view.suffix:.jsp

文件夹结构:

├── pom.xml
├── src
│   └── main
│       ├── java
│       │   └── gt
│       │       └── SpringBootWebApplication.java
│       ├── resources
│       │   └── application.properties
│       └── webapp
│           ├── css
│           │   └── main.css
│           └── jsp
│               └── welcome.jsp

所需的依赖项:

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

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

welcome.jsp

welcome.jsp

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

</html>

@Controller:

@Controller:

    @RequestMapping("/")
    String welcome(Model m) {
        m.addAttribute("msg", "Hello");
        return "welcome";
    }

使用 mvn spring-boot:run

Run using mvn spring-boot:run

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

这篇关于带有Spring Boot的JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆