SpringBoot 刨坑记(一)th:href页面跳转失败

使用跳转静态页面的方法来跳转动态页面会出现 NOT FOUND 404
在这里插入图片描述

在这里插入图片描述

出于安全性的原因考虑,不能直接跳转


→→
→→→

写一个简单的例子来实现页面跳转(动态和静态)

在templates下新建页面,这里用house.html作为主页面
在这里插入图片描述
在static中建立静态页面index6.html
在这里插入图片描述

以下是代码 house.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>house</title>
    <script type="text/javascript">
    </script>
</head>

<body>
	<h1>This is house.html</h1>
	<a href="/html/index6.html">跳转6</a> <!--此处跳转静态页面-->
	<br>
	<a th:href="@{/showtitle}">跳转5</a> <!--此处跳转动态页面-->

</body>
</html>

controller

    @RequestMapping("showtitle")
    public String gotoIndex(){
        return "index5";
    }

springboot会自动去controller中找到showtitle请求,执行其中的内容
在这里插入图片描述

→跳转5

在这里插入图片描述
动态页面的跳转需要先请求服务器,访问后台应用程序,然后再转到页面,在springboot中不建议用jsp,用Thymeleaf来做动态页面。

  • 10
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,下面是实现步骤: 1. 在Web应用程序的“src/main/resources/templates”目录下创建一个名为“success.html”的Thymeleaf模板文件。在该文件中添加以下内容: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Success Page</title> </head> <body> <h2>Success!</h2> </body> </html> ``` 2. 在index.html文件中,添加两个超链接,一个使用Thymeleaf,一个使用常规的HTML链接。如下所示: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Home Page</title> </head> <body> <h2>Welcome to the Home Page</h2> <a th:href="@{/success}">Go to Success Page (Thymeleaf)</a> <br /> <a href="/success">Go to Success Page (HTML)</a> </body> </html> ``` 3. 在Web应用程序的“src/main/resources/application.properties”文件中添加以下内容: ``` spring.mvc.view.prefix=classpath:/templates/ spring.mvc.view.suffix=.html ``` 4. 在Web应用程序的“src/main/java”目录下创建一个名为“com.example.demo”的Java包。 5. 在“com.example.demo”包中创建一个名为“DemoApplication.java”的类,并添加以下内容: ```java package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 6. 在Web应用程序的“src/main/java”目录下创建一个名为“com.example.demo.controller”的Java包。 7. 在“com.example.demo.controller”包中创建一个名为“HomeController.java”的控制器类,并添加以下内容: ```java package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home() { return "index"; } @GetMapping("/success") public String success() { return "success"; } } ``` 现在你可以启动应用程序,并在浏览器中输入http://localhost:8080/来访问首页。点击超链接“Go to Success Page (Thymeleaf)”或“Go to Success Page (HTML)”将跳转到“success.html”页面。 希望这些步骤可以帮助到你实现要求的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值