springboot如何打开jsp页面或者html页面

  1. 打开jsp页面

首先在application.yml文件或application.properties里添加配置

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

接着在pom.xml添加依赖包

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

最后在控制层添加一小段代码(记得给控制层添加@Controller)

 //打开所有jsp页面
    @RequestMapping("/{view}")
    public String html(@PathVariable("view")String view){
        return view;
    }//其中view就是你要打开的视图哦
  1. 打开html页面

首先步骤jsp差不多,一样在application.yml文件添加配置
注意别漏了classpath:,会报错的

 spring:
      thymeleaf:
        prefix: classpath:/templates/
        suffix: .html
        mode: LEGACYHTML5
        cache: false

接着在pom.xml添加模板

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

最后一步跟都一样

 //打开所有html页面
        @RequestMapping("/{view}")
        public String html(@PathVariable("view")String view){
            return view;
        }//其中view就是你要打开的视图哦

然后启动你的项目会默认打开index.html页面哦!

  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页面
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值