SpringBoot跳转页面详解+thymeleaf

SpringBoot跳转页面详解+thymeleafnbsp nbsp nbsp nbsp 初次做 SpringBoot 要解决页面跳转的问题 这个问题我弄了大半天 弄好后 其实也不算个事 写出来给大家提个醒 其实不要使用 springboot 的 RestControll 注解 直接使用 spring 原来的注解 Controller 就可以了 示例如下 Controllerpu nbsp nbsp nbsp RequestMa

        初次做SpringBoot,要解决页面跳转的问题,这个问题我弄了大半天,弄好后,其实也不算个事,写出来给大家提个醒!其实不要使用spring boot的@RestController注解,直接使用spring原来的注解@Controller就可以了。示例如下:

@Controller

public class ActionController {

    @RequestMapping(value = “/action”,method = RequestMethod.GET)

    public String index(){

        return “login”;

    }

}

如果你的项目如什么都没有配,那么你想跳到login.html时,语句必须是  return “login.html”  ,否则它会报错 。因为找不到login文件。

        为什么呢?因为@RestController注解,相当于@Controller+@ResponseBody两个注解的结合, @Responsebody后,返回结果直接写入HTTP response body中,不会被解析为跳转路径,所以你总是看到是打印字符串的效果,不是跳转效果。

=========================================================

        请看更详细的解答:SpringBoot里面没有我们之前常规web开发的WebContent(WebApp),它只有src目录,在src/main/resources下面有两个文件夹,[static]和[templates],springboot默认static中放静态页面,而templates中放动态页面,见下图:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406104541721-1302659819.png

静态页面:

        静态页面可以直接访问。这里我们直接在static放一个hello.html,然后直接输入http://localhost:8080/hello.html便能成功访问(好像可以新建一个public文件夹,也可以放静态文件)。

        也可以通过controller跳转:

@Controller

public class HelloController {

    @RequestMapping(“/Hi”)

    public String sayHello() {

        return “hello.html”;

    }

}

        然后输入http://localhost:8080/Hi就可以成功访问 

动态页面:

        动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问JSP。spring boot建议不要使用JSP,默认使用Thymeleaf来做动态页面。

要在pom中要添加Thymeleaf组件

        


            

org.springframework.boot

            

spring-boot-starter-thymeleaf

        



但你加上了上面的thymeleaf后,你必须重启工程,即使是你配置了热启动后,也要重启工程,才可以看到效果 。

    我们先在tempates文件夹中也新建一个hello.html但内容不同,然后先试一下直接访问该页面。输入http://localhost:8080/hello.html:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406105509468-1935985534.png

结果显然访问的是静态问价夹里面的那个hello.html

然后我们现在再试一下用controller:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406105655969-540195122.png

似乎无法访问到hello.html了。。。这是因为:静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/index.html,注意看两者return代码也有区别,动态有或没有html后缀都可以。

也就是我们要这样改controller:

@Controller

public class HelloController {

@RequestMapping(“/Hi”)

    public String sayHello() {

        return “hello”;//在没有配置的情况下,return “hello”; 或者return “hello”都可以,它们都会到templates/index.html去。

    }  

}

然后就可以成功跳转了

然后我们看看返回一点数据在前端利用Thyemleaf来拿:

@Controller

public class HelloController {

 

    @RequestMapping(“/Hi”)

    public ModelAndView sayHello() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(“hello”);

        modelAndView.addObject(“key”, 12345);

        //System.out.println(“test”);

        return modelAndView;

    }

   

}

———————

Templates/hello.html

———————


Insert title here

 

this is the hello.html in templates

 

 

效果:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406115915727-969075466.png

 

 

 

如果不想返回视图,则用@RestController

如果用了静态模板你还想返回static中的页面,那么就要用重定向:

如果在使用动态页面时还想跳转到/static/index.html,可以使用重定向return “redirect:/index.html”。 

 

几点tips:

1.拦截的url最后不要跟视图重合,否则会抛出Circular view path异常,我之前就是

@Controller

public class HelloController {

 

    @RequestMapping(“/hello”)

    public String sayHello() {

        return “hello.html”; 

    }

   

}

然后就报错说会有个循环视图的错误,反正以后注意就是。

2.每次改完都要重新停止应用,再重新启动很烦~但springboot有个叫热部署的东西,就是说在项目中修改代码可以不用重新停止应用再重新启动,可以自动重启,这里我们用的是devtools:

方法见网址:https://www.cnblogs.com/cx-code/p/8686453.html,摘录如下:

sts热部署,即是在项目中修改代码不用重新启动服务,提高效率。

方法如下:

1.pom文件中引入  devtools  依赖:

    



  

org.springframework.boot

  

spring-boot-devtools

  

  
true






 

2.application.properties  文件中码上以下内容:

spring.thymeleaf.cache=true        //缓存
spring.devtools.restart.enabled=true   //开启
spring.devtools.restart.additional-paths=src/main/java  //监听目录

3.原理

















———————————–

如何使用thymeleaf [taim li:f] 来达到以前jsp获取Model值这样的效果(thymeleaf是什么?你自行百度),那么,你得在maven中加依赖:

   

org.springframework.boot

   

spring-boot-starter-thymeleaf

 

然后,在配置文件:application.yml或application.property中加配置:

spring.thymeleaf.prefix:classpath: /templates/

spring.thymeleaf.suffix: .html

spring.thymeleaf.mode: HTML

spring.thymeleaf.encoding: utf-8

spring.thymeleaf.cache: false

 

再写控制类时把Model加在参数中:

它就会跳转到/templates/index.html这个网页上去了。

你的index.html如果想显示”我是一个兵”,”来自老百姓”的值的话,网页应该这样写:

AAAAindex.jspsdfasd

Today is: 13 february 2011.

  

@{${aaa}}


${aaa}
   

${bbb}       

——————————————-










版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211436.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:26
下一篇 2026年3月18日 下午10:26


相关推荐

  • myeclipse10 64 安装svn插件的方式

    myeclipse10 64 安装svn插件的方式方法一:在线安装1.打开HELP->MyEclipseConfigurationCenter。切换到SoftWare标签页。2.点击AddSite打开对话框,在对话框Name输入Svn,URL中输入:http://subclipse.tigris.org/update_1.6.x3.在左边栏中找到PersonalSite中找到SVN展开。将CoreSVNK

    2022年7月20日
    16
  • JavaScript数组合并,参数解构方法

    JavaScript数组合并,参数解构方法JavaScript 两个数组数据合并 vararr1 1 2 3 vararr2 4 5 6 1 利用对象参数解构 arr1 push arr2 console log arr1 1 2 3 4 5 6 arr2 ES6 新语法 类似 Python 的位置参数的简写 将 arr2 分解成单个位置参数传入 pu

    2026年3月18日
    2
  • Python break 和 continue 语句

    Python break 和 continue 语句在Python中,break和continue语句用于改变普通循环的流程。通常情况下,循环遍历一段代码,直到判断条件为False。但有时,可能会希望不检测判断条件就可以终止当前迭代,甚至是整个循环。这种情况下,就需要使用break和continue语句。

    2022年6月10日
    30
  • vscode html注释快捷键_宇宙最强vscode教程(基础篇)

    vscode html注释快捷键_宇宙最强vscode教程(基础篇)本文主要介绍vscode在工作中常用的快捷键及插件,目标在于提高工作效率本文的快捷键是基于mac的,windows下的快捷键放在括号里Cmd+Shift+P(winCtrl+Shift+P)零、快速入门有经验的可以跳过快速入门或者大致浏览一遍1.命令面板命令面板是vscode快捷键的主要交互界面,可以使用f1或者Cmd+Shift+P(winCtrl+Shift+P)打开。在命令…

    2022年6月9日
    52
  • H5添加QQ好友的链接

    tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=你的QQ号&website=www.oicqzone.com既点击有如下效果:

    2022年4月17日
    109
  • CoreDNS介绍

    CoreDNS介绍本文介绍 CoreDNS 相关配置以及验证方法 实验环境为 Kubernetes1 11 搭建方法参考 kubeadm 安装 kubernetesV1 11 1 集群 busybox 的槽点开始之前先吐槽一下 busybox 中的 nslookup 命令 这个命令应该是实现的不是很完全 导致我在测试 DNS 的成功 得到了错误的信息 先来看一下 root devops 101 kubec

    2026年3月19日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号