我已做好框架 但是在写thymeleaf的时候,下边return返回的应该是一个html文件里边的内容,我确认路径没有错误,但是返回的确是我在return上写的路径。这个怎么解决啊
代码怎么写的呢
你可以尝试一下如下方法解决
①是否存在模板引擎的问题,有的模板引擎默认返回是会加上后缀啥的
②检查是否加了转发或者重定向的关键字
学过vue的同学看这个知识点可能一下就熟悉了,类似于vue将公用的标签放到一个组件中,到时候进行调用该组件。一个道理
我们发现一个问题,顶部都相同,我们是不是应该将它抽取出来呢?
我们把 dashboard 页面中属于公共的 nav 部分抽取出来放到commons.html中body标签中,也就是这部分内容
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" >
<!--后台主页显示登录用户的信息-->
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">[[${session.loginUser}]]</a>
<input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
<ul class="navbar-nav px-3">
<li class="nav-item text-nowrap">
<a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">注销</a>
</li>
</ul>
</nav>
同时也删除dashboard.html,list.html
文件中的这部分代码
接下来在commons.html中nav标签加入抽取片段的属性
注意,此时我们运行项目会发现项目的顶部内容是没有的,因为我们还需要把抽取出来的片段引入
我们要把抽取出来的片段引入
<!--引入抽取的topbar-->
<!--模板名 : 会使用thymeleaf的前后缀配置规则进行解析
使用~{模板::标签名}-->
<div th:replace="~{commons/commons::topbar}"></div>
我们在dashboard.html 和list.html 中刚刚删掉头部导航栏的地方加上如上代码
可以看到已经加载过来了
没有返回页面吗?
分三个步骤自己排查一下
1.确认一下引入
org.springframework.boot
spring-boot-starter-thymeleaf
2.确认一下配置开启配置了没
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
3.有没有添加Thymeleaf 的视图解析器:
怕不是在controller上加的是RestController注解吧