一,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)