spring boot 默认使用 thymeleaf 作为模板引擎,直接在 template 目录中存放 jsp 文件并不能正常访问,需要在 main 目录下新建一个文件夹来存放 jsp 文件,而且需要添加依赖。
首先在 main
目录下新建一个 webapp
目录(任何名称都可以),然后在 project structure 中将它添加到 web resource directory。
在 pom.xml 中添加依赖以支持 jstl 和 jsp:
<dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> </dependency> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> </dependency>
编辑 application.yml:
spring: mvc: view: suffix: .jsp prefix: /view/
设置前缀为 jsp 文件存放的相对路径(这里将 jsp 文件放在 view
目录),后缀为 .jsp
。
indexcontroller
:
import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; @controller public class indexcontroller { @requestmapping("/") public modelandview index() { modelandview index = new modelandview("index"); index.addobject("message", "hello, spring boot!"); return index; } }
index.jsp
:
<%@ page contenttype="text/html;charset=utf-8" language="java" %> <html> <head> <title>index</title> </head> <body> <h1>spring boot with jsp</h1> <h2>${message}</h2> </body> </html>
访问 http://www.lhsxpumps.com/_localhost:8080/
:
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论