SpringBoot怎么实现转页功能

发布时间:2023-11-03 10:59:09 作者:栢白
来源:亿速云 阅读:128

今天小编给大家分享的是SpringBoot怎么实现转页功能,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。


内部转页forward

转页配置

在 配置文件 resources > application.properties 中可以找到转页的配置信息,

这些是SpringBoot的默认配置, 是可以省略不写在配置文件中的

#在构建URL时添加到视图名称前的前缀(默认值: classpath:/templates/ )

spring.thymeleaf.prefix=classpath:/templates/

#在构建URL时添加到视图名称后的后缀(默认值:.html )

spring.thymeleaf.suffix=.html

准备页面

在 resources 文件夹下 创建新文件夹 templates , 这个文件夹是springboot默认存放模板页面的文件夹

在文件夹下建立 目标页面 ref.html

SpringBoot怎么实现转页功能

同样 在创建文件时, 同时创建了所属的文件夹,

当然也可以分开创建 , 文件夹 Directory

SpringBoot怎么实现转页功能

创建后, 添加一句 “hello spring boot”

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    hello spring boot
</body>
</html>

在方法中添加转页

方法是通过返回值来进行转页的, 添加新的方法

String 返回类型对应的返回值 就是 转页路径, 本例中应该是 templates > ref.html

 	@RequestMapping("/test/test02")
    public String test02(){
        System.out.println(" controller 中的测试方法 test 02 ");
        return "转页的路径";
    }

得到页面路径

通过 菜单选择项得到 页面路径

文件上 右键 > Copy Path&hellip; > Path From Source Root 将路径复制到剪切板上

SpringBoot怎么实现转页功能

将信息替换返回值,

注意要 掐头( templates/ ) 去尾 ( .html ) 因为这些信息已经在配置文件中指定了

@RequestMapping("/test/test02")
    public String test02(){
        System.out.println(" controller 中的测试方法 test 02 ");
        return "ref";
    }

测试

重新启动项目, 在浏览器地址栏输入URL : http://localhost:8080/test/test02

看到 ref.html 页面上添加的信息, 测试成功

SpringBoot怎么实现转页功能

重新定向redirect

就是方法执行后, 不是返回页面, 而是跳转到别的方法里, 继续执行

添加新的方法

添加测试方法 test03

返回值 增加 redirect: 关键字

后面是新的请求URL, 本例是 test02,

如果当前方法的请求前缀与重新定向的方法前缀相同( 本例 前缀为 /test ), 可以省略

特别注意 “:” 与后面的URL之间不能有空格

    @RequestMapping("/test/test03")
    public String test03(){
        System.out.println(" controller 中的测试方法 test 03 ");
        return "redirect:test02";
    }

测试

重新启动项目, 在浏览器地址栏输入URL : http://localhost:8080/test/test03

看到 ref.html 页面上添加的信息, 注意URL又从 test03 跳转 成 test02, 说明依然还是 test02 方法转页的

SpringBoot怎么实现转页功能

但在idea控制台能看到两个方法被 依次执行

SpringBoot怎么实现转页功能

简单转页

当 只是简单的进行转页, 没有具体业务代码时, SpringBoot 提供了简单的转页方式

注意 :

类上加 注解 @Configuration说明这是一个配置类, 项目启动时会优先读取类中的配置信息

类 实现 WebMvcConfigurer 接口 , 并覆盖 addViewControllers() 方法

通过传入 ViewControllerRegistry 类型的参数, 来实现内部转页, 重新定向

package com.yuan.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 内部转页
        registry.addViewController("/").setViewName("start");
        // 重新定向
        registry.addRedirectViewController("/test03", "/");        
    }
}

页面发请求的三种方式

在 resources > templates 文件夹下 增加新的页面 start

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    welcome start <br>
</body>
</html>

由于 在 WebMvcConfig 配置类 配置了默认请求 “/” 转到 start.html, 所以启动项目后默认打开 start页面

SpringBoot怎么实现转页功能

<a>超链接

在start.html 里增加 超链接 , 通过 href 属性 对 test02 发请求, 转页到 ref.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    welcome start <br>
    <a href="/test/test02" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >test02</a> <br>
</body>
</html>

重启项目, 浏览器上可以看到超链接

SpringBoot怎么实现转页功能

点击超链接测试, 可以转到ref.html页面

SpringBoot怎么实现转页功能

form表单

在 start.html 里增加 表单, 通过 action 属性 对 test02 发请求, 转页到 ref.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    welcome start <br>
    <a href="/test/test02" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >test02</a> <br>
    <form action="/test/test02" >
        <input type="submit" value="提交">
    </form>
</body>
</html>

重启项目, 浏览器上可以看到表单的 submit 提交按钮

SpringBoot怎么实现转页功能

点击提交按钮, 可以转到ref.html页面

SpringBoot怎么实现转页功能

location.href

在 start.html 里增加 按钮 , 通过 按钮的 onclick单击事件 调用函数,

从而通过 location.href属性 对 test02 发请求, 转页到 ref.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    welcome start <br>
    <a href="/test/test02" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >test02</a> <br>
    <form action="/test/test02"  method="post" >
        <input type="submit" value="提交">
    </form>
    <button type="button" onclick="fn()" >按钮</button>
</body>
<script type="text/javascript" >
    function fn() {
        window.location.href = "/test/test02"
    }
</script>
</html>

重启项目, 浏览器上可以看到按钮

SpringBoot怎么实现转页功能

点击按钮, 可以转到ref.html页面

SpringBoot怎么实现转页功能

关于SpringBoot怎么实现转页功能就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

推荐阅读:
  1. SpringBoot+Spring Cloud Consul服务注册的方法
  2. springboot集成swagger的步骤是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:Golang爬虫框架colly使用的方法介绍

下一篇:Golang json 库中的RawMessage功能原理介绍

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》