spring boot跳转html

本文详细介绍了如何在SpringBoot项目中整合Thymeleaf模版引擎,包括依赖添加、配置设置及控制器代码示例。同时,对比了使用静态HTML页面的方式,为读者提供了全面的参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、因为springboot不赞成使用jsp页面,因此考虑spring boot推荐的Thymeleaf模版

添加模版

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

application.properties添加如下配置:(这个不配置也可以的,如果不配置,会到resources/templates下去找同名的模板或者页面)
# 定位模板的目录
spring.mvc.view.prefix=classpath:/templates/
# 给返回的页面添加后缀名
spring.mvc.view.suffix=.html

2、设计一个controller代码

package com.example.demo.controller;

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

/**
 * @Auther: Matthew
 * @Date: 2018/12/4 09:12
 * @Description:
 */

@Controller
@RequestMapping(value = "/oa")
public class Handle {

    /**
     * 存入数据模型,并返回跳转的页面
     * @param model
     * @return String
     */
    @RequestMapping(value = "/login")
    public String index(Model model) {

        System.out.print("进来了");
        model.addAttribute("title", "the first spring boot");
        model.addAttribute("name", "楚苓大妈");

        return "index";
    }
}

模板使用demo
          <h1 id="title">${title}</h1>
  模板中的各类语法自己搜索,这里不做介绍
 
  注意,spring boot默认开启了静态文件的配置,任何放在static文件夹下的资源都是静态文件。引用静态文件时以/或者前缀不加任何定位符,都会去static文件夹下查找。
 
  2,实际项目中,现在没有使用模板了。都是使用的静态界面html
  和模板差不多
  # 定位页面的目录到static/下
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
controller当然也是需要的,和之前一样:
@Controller//这里只能用这个

@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
 

1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。

例如:本来应该到success.html页面的,则其显示success.

2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。

3)如果需要返回json或者xml或者自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解

 
 
 
### Spring Boot 中实现页面跳转的方法 在 Spring Boot 的开发过程中,可以通过多种方法实现页面跳转。以下是基于 Thymeleaf 模板引擎的示例代码以及具体实现方式。 #### 使用 Thymeleaf 渲染 HTML 页面 Thymeleaf 是一种常用的模板引擎,在 Controller 层中可以轻松实现页面跳转并传递数据给前端。以下是一个简单的例子: ```java @Controller public class PageController { @GetMapping("/home") public String homePage(Model model) { model.addAttribute("message", "欢迎访问首页!"); return "index"; // 返回名为 index.html 的视图文件 } @PostMapping("/redirect") public String redirectToAnotherPage() { return "redirect:/another-page"; // 重定向到另一个 URL } } ``` 上述代码展示了两种常见的页面跳转方式: 1. **直接返回视图名称**:当请求 `/home` 时,会加载 `src/main/resources/templates/index.html` 文件,并将模型中的属性传递过去[^3]。 2. **使用 redirect 关键字**:通过 `return "redirect:/another-page"` 可以实现 HTTP 重定向操作[^1]。 #### 配置 Thymeleaf 支持 为了使以上代码正常工作,需确保项目的 pom.xml 或 build.gradle 文件中已引入 Thymeleaf 依赖项。例如 Maven 配置如下所示: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 完成配置后,默认情况下,Spring Boot 将自动扫描位于 `templates/` 目录下的 `.html` 文件作为可用视图资源。 --- ### 注意事项 如果未启用任何模板引擎,则无法直接返回 HTML 文件名;此时可考虑设置静态资源路径或将目标文件放置于默认静态目录下(如 `static/`, `public/`),并通过响应实体对象的方式提供服务。 ```java @RestController @RequestMapping("/api/pages") public class StaticPageController { @GetMapping("/example") public ResponseEntity<Resource> serveStaticHtmlFile() throws IOException { Path path = Paths.get("path/to/static/example.html"); Resource resource = new UrlResource(path.toUri()); if (resource.exists()) { return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + resource.getFilename() + "\"") .body(resource); } else { throw new FileNotFoundException("File not found at specified location!"); } } } ``` 此片段适用于处理特定场景需求,比如动态生成或外部托管的内容链接[^2]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matthew_leung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值