SpringBoot怎么实现转页功能

不想看,AI帮我总结一下文章

今天小编给大家分享的是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怎么实现转页功能  springboot 第1张

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

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

SpringBoot怎么实现转页功能  springboot 第2张

创建后, 添加一句 “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怎么实现转页功能  springboot 第3张

将信息替换返回值,

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

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

测试

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

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

重新定向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怎么实现转页功能  springboot 第4张

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

SpringBoot怎么实现转页功能  springboot 第5张

简单转页

当 只是简单的进行转页, 没有具体业务代码时, 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怎么实现转页功能  springboot 第6张

<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怎么实现转页功能  springboot 第7张

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

SpringBoot怎么实现转页功能  springboot 第8张

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怎么实现转页功能  springboot 第9张

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

SpringBoot怎么实现转页功能  springboot 第10张

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>

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

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

SpringBoot怎么实现转页功能  springboot 第11张

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

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

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接