[JAVA] SpringBoot 转发请求至指定页面的操作方法

2467 0
黑夜隐士 2022-11-8 16:46:36 | 显示全部楼层 |阅读模式
目录

    1、前言2、多种方式尝试
      2.1 redirect、forward关键字(不是 Java 关键字)2.2 重写 WebMvcConfigurer 接口中的方法2.3 引入 thymeleaf
    3、最终效果


1、前言

原先的页面访问地址为:http://127.0.0.1:8888/office/schdule/index/,
重构项目,SpringBoot 项目,前后分离,前端文件放置静态目录(static)下,访问地址:http://127.0.0.1:8888/office/schdule/index.html。可以看出,原先是请求接口进行转发到指定页面的,而现在是直接访问静态文件,为了能兼容原先访问地址,则需对请求进行转发处理。

2、多种方式尝试


2.1 redirect、forward关键字(不是 Java 关键字)
  1. /**
  2. * 跳转控制器
  3. * @author pky
  4. */
  5. @Controller
  6. public class JumpController extends BaseResultController {
  7.     /**
  8.      * 跳转主页
  9.      * @return 要转发的页面地址
  10.      */
  11.     @GetMapping("html/index")
  12.     public String forwordIndexHtml() {
  13.         return "forword:/schdule/index.html";
  14.     }
  15.         /**
  16.      * 跳转主页
  17.      * @return 要重定向的页面地址
  18.      */
  19.     @GetMapping("html/index")
  20.     public String redirectIndexHtml() {
  21.         return "redirect:/schdule/index.html";
  22.     }
  23. }
复制代码
注意:需要使用@Controller,不可使用@RestController。
@RestController相当于@Controller和@ResponseBody合在一起的作用,如果使用@RestController注解Controller层的话,则返回的是return里面的内容,无法返回到指定的页面,配置的视图解析器InternalResourceViewResolver也就自然没有作用了。
然而,上述代码报了Cannot resolve MVC View ‘forword:/schdule/index.html’无法解析视图的错误

2.2 重写 WebMvcConfigurer 接口中的方法

创建 WebConfig 类实现 WebMvcConfigurer 接口,重写 addViewControllers 方法。如下代码:
  1. import org.springframework.boot.SpringBootConfiguration;
  2. import org.springframework.web.servlet.config.annotation.*;
  3. /**
  4. * Web 配置类
  5. *
  6. * @author pky
  7. */
  8. @SpringBootConfiguration
  9. public class WebConfig implements WebMvcConfigurer {
  10.     @Override
  11.     public void addViewControllers(ViewControllerRegistry registry) {
  12.         // 设置访问相对路径及其对应的 HTML 文件的相对路径(相对于静态目录)
  13.         registry.addViewController("html/index").setViewName("schdule/index.html");
  14.     }
  15. }
复制代码
依然还是报了Cannot resolve MVC View ‘forword:/schdule/index.html’无法解析视图的错误

2.3 引入 thymeleaf

以上的错误是因为没有使用模板引擎,无法识别静态页面。
pom.xml
  1.    <!--    模板引擎   -->
  2.    <dependency>
  3.        <groupId>org.springframework.boot</groupId>
  4.        <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5.    </dependency>
复制代码
applicaiton.properties
  1. server.servlet.context-path=/office/
  2. server.port=10000
  3. # 静态资源路径
  4. spring.resources.static-locations=classpath:/static/, classpath:/templates/
复制代码
值得注意的是,SpringBoot 默认的静态目录是resources下的 static,引入thymeleaf模板引擎后,还是无法识别static下的文件,只能识别templates下的文件。因此applicaiton.properties需配置templates资源路径地址,如下图index.html路径:


修改 Controller
去掉forwordredirect关键字,直接写需跳转的地址。
  1. /**
  2. * 跳转控制器
  3. * @author pky
  4. */
  5. @Controller
  6. public class JumpController extends BaseResultController {
  7.     /**
  8.      * 跳转主页(使用template模板引擎)
  9.      * @return 要转发到指定页面的相对地址(相对于 templates)
  10.      */
  11.     @GetMapping("html/index")
  12.     public String forwordIndexHtml() {
  13.         return "schdule/index.html";
  14.     }
  15. }
复制代码
另外,引入了引入thymeleaf模板引擎后,上述addViewControllers(ViewControllerRegistry registry)方法也能在 IDEA 中正常跳转到指定地址,但是运行时,却总是报找不到静态文件,无论怎么修改路径都不行。具体原因不明,因此不采用。

3、最终效果

http://127.0.0.1:8888/office/schdule/index/


http://127.0.0.1:8888/office/schdule/index.html


到此这篇关于SpringBoot 转发请求至指定页面的文章就介绍到这了,更多相关SpringBoot 转发请求至指定页面内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行