springboot访问jsp页面

配置pom

springboot2.x之后不推荐使用jsp,所以需要手动加依赖

<!--用于解析jsp页面-->
<dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
 </dependency>
 <dependency>
     <groupId>org.apache.tomcat</groupId>
     <artifactId>tomcat-jsp-api</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
     <scope>provided</scope>
 </dependency>

或者

<!-- jsp访问/jar加载 -->
<dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
     <scope>provided</scope>
 </dependency>

 <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
 </dependency>

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

都可以
验证过的springboot版本:2.1.3.RELEASE 2.7.x

<resources>
     <!--加载webapp下的资源,比如jsp-->
      <resource>
          <directory>src/main/webapp</directory>
          <targetPath>META-INF/resources</targetPath>
          <includes>
              <include>**/**</include>
          </includes>
          <filtering>false</filtering>
      </resource>
      <!--加载resources下的资源-->
      <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
      </resource>
      <!--加载java文件夹下的资源,比如xml写在java文件夹下的mapper,或者其他类型的文件资源-->
      <resource>
          <directory>src/main/java</directory>
          <includes>
              <include>**/*.xml</include>
              <include>**/*.json</include>
              <include>**/*.ftl</include>
          </includes>
      </resource>
</resources>

jsp路径问题

在这里插入图片描述
这是jsp所在的路径,可以看到是在WEB-INF/jsp下的,那么访问可以这样:
在这里插入图片描述
如果不想这么麻烦,只返回success就可以的话,可以在配置文件中配置一下,前后缀:
注意:值不要加双引号,否则找不到文件
在这里插入图片描述
一定要这样:

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

在这里插入图片描述

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
springboot中使用Thymeleaf模板引擎来实现跳转到jsp页面。 首先,确保在pom.xml文件中引入了Thymeleaf以及Spring Boot相关的依赖。然后在application.properties文件中配置Thymeleaf的相关属性。 接下来,在Spring Boot的Controller中定义一个请求处理方法,使用@GetMapping或@PostMapping注解来映射URL路径。在方法中,通过返回一个字符串类型的视图名称来实现跳转。 例如,假设我们有一个UserController类,其中有一个方法用于处理"/user"路径的请求: ```java @Controller public class UserController { @GetMapping("/user") public String showUser(Model model) { // 处理业务逻辑,将数据存入model对象中 model.addAttribute("username", "John Doe"); return "user"; // 返回user作为视图名称 } } ``` 注意,跳转的jsp页面的名称应与返回的视图名称对应。同时,该jsp文件需要保存在src/main/resources/templates目录下。 在jsp页面中,可以使用Thymeleaf的语法来显示从Controller传递的数据。例如,在user.jsp中可以使用如下代码显示用户名: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>User Page</title> </head> <body> <h1>Welcome, <span th:text="${username}"></span>!</h1> </body> </html> ``` 在浏览器中访问"/user"路径,就可以看到跳转到user.jsp页面并显示用户名的效果。 通过上述步骤,我们就可以在Spring Boot中使用Thymeleaf模板引擎来实现跳转到jsp页面

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值