一,thymeleaf如何给fragment传递参数?

1,如果是全局的参数,可以用interceptor中传递

非全局参数,可以从controller中传递

2,引用片断时也可以传递参数

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,演示项目的相关信息

1,项目地址:

https://github.com/liuhongdi/fragmentparam

2,项目功能:

用一个页面header的例子,

演示了给fragment传递参数

3,项目结构,如图:

三,配置文件说明

1,pom.xml

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--thymeleaf begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2,application.properties

#error
server.error.include-stacktrace=always
#error
#logging.level.org.springframework.web=trace
logging.level.org.springframework.web=debug
#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

四,java代码说明

1,HomeController.java

@RequestMapping("/home")
@Controller
public class HomeController { //传递参数给模板
@GetMapping("/home")
public String index(ModelMap modelMap) {
modelMap.addAttribute("curTitle","首页");
modelMap.addAttribute("jsversion","20200915121212");
return "home/home";
}
}

2,DefaultMvcConfig.java

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class DefaultMvcConfig implements WebMvcConfigurer { @Resource
private WebInterceptor webInterceptor; //添加Interceptor
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(webInterceptor)
.addPathPatterns("/home/**","/goods/**","/login/login","/order/**","/set/**","/admin/**","/merchant/**")
.excludePathPatterns("/html/*","/js/*");
}
}

配置interceptor

3,WebInterceptor.java

@Component
public class WebInterceptor extends HandlerInterceptorAdapter {
//如果view不为空,把登录信息传递给模板
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
if (modelAndView != null) {
ModelMap modelMap = modelAndView.getModelMap();
modelMap.addAttribute("GlobalTitle","商品管理系统");
}
}
}

传递参数给模板

4,header.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head th:fragment="common_header(jsversion,csslink)">
<title>[[${curTitle}]]-[[${GlobalTitle}]]</title>
<!--全局通用框架样式 begin-->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta charset="utf-8" />
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<script type="text/javascript" language="JavaScript" th:src="@{/js/utils.js(${jsversion})}" ></script>
<th:block th:replace="${csslink}" />
</head>

说明:两个参数:curTitle,GlobalTitle是java直接传递到模板的

另两个参数: jsversion,csslink是通过fragment传递的参数

5,home.html

<!DOCTYPE html>
<html lang="en">
<head th:replace="common/header :: common_header(${jsversion},~{::link})">
<link rel="stylesheet" href="/css/home.css" />
<link rel="stylesheet" href="/css/home2.css" />
</head>
<body>
<div style="width:100%;height:30px;background:#ffffff;font-size: 16px;" ></div>
<div id="content" style="width:1040px;">
<div style="width:790px;float:left;margin-left:30px;">
<!--main begin-->
this is home
<!--main end-->
</div>
</div>
</body>
</html>

说明:在引用fragment时,jsversion这个参数,我们直接取java传递的值,

::link则表示取当前元素下面的link元素,两个都会传递给fragment

五,测试效果

1,访问:

http://127.0.0.1:8080/home/home

返回:

可以看到title中包含的两个参数已起作用

查看源码:

<!DOCTYPE html>
<html lang="en">
<head>
<title>首页-商品管理系统</title>
<!--全局通用框架样式 begin-->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta charset="utf-8" />
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<script type="text/javascript" language="JavaScript" src="/js/utils.js?20200915121212" ></script>
<link rel="stylesheet" href="/css/home.css" /><link rel="stylesheet" href="/css/home2.css" />
</head>
<body>
<div style="width:100%;height:30px;background:#ffffff;font-size: 16px;" ></div>
<div id="content" style="width:1040px;">
<div style="width:790px;float:left;margin-left:30px;">
<!--main begin-->
this is home
<!--main end-->
</div>
</div>
</body>
</html>

可以看到:jsversion的传递生效,

两个css文件的传递也生效

六,查看spring boot版本

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)

spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)的更多相关文章

  1. 如何向一个Fragment传递参数---setArguments方法的介绍

    在我们平常开发中经常会用到Fragment,当我们使用Fragment时一般是通过new Fragment的构造方法来实现,如果我问你怎么向一个Fragment传递参数,你是不是会首先想到通过构造方法 ...

  2. springMVC controller间跳转 重定向 传递参数的方法

    springMVC controller间跳转 重定向 传递参数的方法 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参 ...

  3. android fragment传递参数_fragment之间传值的两种方法

    在Activity中加载Fragment的时候.有时候要使用多个Fragment切换.并传值到另外一个Fragment.也就是说两个Fragment之间进行参数的传递.查了很多资料.找到两种方法.一种 ...

  4. C++向main函数传递参数的方法(实例已上传至github)

    通常情况下,我们定义的main函数都只有空形参列表: int main(){...} 然而,有时我们确实需要给mian传递实参,一种常见的情况是用户设置一组选项来确定函数所要执行的操作.例如,假定ma ...

  5. jsp中四种传递参数的方法

    jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...

  6. Jsp传递参数的方法

    今天老师讲了jsp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用! 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超 ...

  7. Javascript 定时器调用传递参数的方法

    文章来源:  https://m.jb51.net/article/20880.htm 备注:先记下,以后整理: Javascript 定时器调用传递参数的方法,需要的朋友可以参考下. 无论是wind ...

  8. JSP页面之间传递参数的方法有哪些?

    JSP页面之间传递参数的方法有哪些? 解答: 1)request 2)session 3)application 4)提交表单 5)超链接

  9. php cli传递参数的方法

    php cli传递参数的方法 <pre>$options = "f:g:"; $opts = getopt( $options ); print_r($opts); & ...

随机推荐

  1. LG P2389 电脑班的裁员

    Description ZZY有独特的裁员技巧:每个同学都有一个考试得分$a_i(-1000 \leq a_i \leq 1000)$,在$n$个同学$(n \leq 500)$中选出不大于$k$段$ ...

  2. 乔悟空-CTF-i春秋-Web-SQL

    2020.09.05 是不是有些题已经不能做了--费了半天,到最后发现做不出来,和网上大神的方法一样也不行,最搞笑的有个站好像是别人运营中的,bug好像被修复了-- 做题 题目 题目地址 做题 尝试简 ...

  3. Prometheus Metrics 设计的最佳实践和应用实例,看这篇够了!

    Prometheus 是一个开源的监控解决方案,部署简单易使用,难点在于如何设计符合特定需求的 Metrics 去全面高效地反映系统实时状态,以助力故障问题的发现与定位.本文即基于最佳实践的 Metr ...

  4. Windows实战(1):Nginx代理设置及负载均衡配置

    简言 以下配置实现功能: 反向代理 通过轮询的方式实现nginx负载均衡 直接看以下配置文件: #user nobody; worker_processes 1; #error_log logs/er ...

  5. flutter权限管理permission_handler

    flutter权限管理permission_handler 添加依赖 #权限 permission_handler: ^3.0.0 使用 在android的mainfest中添加权限: <use ...

  6. CPF 入门教程 - 绘图(四)

    CPF NetCore跨平台UI框架,增加了Vlc支持跨平台播放视频. 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) CPF 入门教程 - 样式和动画(三) CPF ...

  7. 1.2Hadoop概述

  8. Java随谈(二)对空指针异常的碎碎念

    本文适合对 Java 空指针痛彻心扉的人阅读,推荐阅读时间25分钟. 若有一些Java8 函数式编程的基础可以当成对基础知识的巩固. 一.万恶的null 今天,我们简单谈谈null的问题.因为null ...

  9. Xmind 2020 破解教程

    前言: 今天用xmind试用版记了会笔记,发现哎哟还真好用,于是乎我脑子一热,点击激活,发现年费好尼玛贵,瞬间我就冷静下来了. 于是乎,脑海里立马浮现出两个字:破解!好了废话不多说,直接上傻瓜教程.( ...

  10. 原创-公司项目部署交付环境预检查shell脚本

    大型项目环境预检查脚本,根据自己实际情况修改脚本中变量,给大家一个思路~ #!/usr/bin/env bash root=$( cd $(dirname $0) pwd ) source " ...