SpringBoot 的静态资源位置

本文介绍如何在Spring Boot项目中集成Thymeleaf模板引擎,包括依赖配置、静态资源路径设置及示例代码解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.没有thymeleaf的情况下

    dependencies {
        	 // Test
        	testCompile 'junit:junit:4.12'
        	testCompile 'org.glassfish.web:javax.el:2.2.4'
        	testCompile 'org.springframework.boot:spring-boot-starter-test:1.4.1.RELEASE'
        	// Spring
        	compile 'org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE'
        	compile 'org.springframework.boot:spring-boot-devtools:1.4.1.RELEASE'
        	// web
        	compile 'org.springframework.boot:spring-boot-starter-web'
     }

默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources) 

html放到其中可以自动访问

2.thymeleaf的情况下

    dependencies {
        	 // Test
        	testCompile 'junit:junit:4.12'
        	testCompile 'org.glassfish.web:javax.el:2.2.4'
        	testCompile 'org.springframework.boot:spring-boot-starter-test:1.4.1.RELEASE'
        	// Spring
        	compile 'org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE'
        	compile 'org.springframework.boot:spring-boot-devtools:1.4.1.RELEASE'
 			// web
        	//compile 'org.springframework.boot:spring-boot-starter-web'
			// Thymeleaf
        	compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
     }
    ext['thymeleaf.version'] = '3.0.9.RELEASE'
    ext['thymeleaf-layout-dialect.version'] = '2.3.0'

除了html之外的静态资源位置不变,html 放到/templates下

  @RequestMapping("t1")
  public String thymeleaf1(ModelMap m) {
    System.out.println("t1");
    User u = new User();
    u.setName("zhangsan");
    u.setPassword("123456");

    m.addAttribute("user", u);
    return "thymeTest";
  }
<!DOCTYPE html>
<html   xmlns:th="http://www.thymeleaf.org">
<head >
<meta charset="UTF-8">
<head th:replace="layout/base :: common_header(~{::title},~{::link})">
  <title>Awesome - Main</title>
  <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
  <link rel="stylesheet" th:href="@{/themes/smoothness/jquery-ui.css}">
</head>
<body>
	hello thymeleaf<br>
	<img src="/Tulips.jpg" style="width:100px;">
	<span th:text="${user.name}"></span>
	<span lang="zh_CN"  th:text="#{title.hello}"></span>
	<a th:href="@{/home/{username}(username=${user.name})}">a1</a>
	<a th:href="|/home/${user.name}|">a2</a>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值