SpringBoot(二):集成thymeleaf与通用js、css的引入以及默认页面

SpringBoot集成thymeleaf

1.首先pom文件中已经有spring-boot-starter-parent与spring-boot-starter-web配置。

2.在pom文件中加入以下依赖

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

3.在appliaction.properties中添加一下配置

spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=LEGACYHTML5
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false

4.在resources下创建templates文件夹,在该文件夹下创建html文件即可。

 

 

thymeleaf页面模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>thymeleaf template</title>
</head>
<body>
    <h1>Thymeleaf模板文件</h1>
</body>
</html>

公共页面元素的抽取

1.首先写一个head.html,内容如下,其中要加入的js,css可以根据自己的需求添加

<head th:fragment="commonHead(title)">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title th:text="${title}">Title</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

<!-- jquery ui组件引用  提示框-->
<link rel="stylesheet" type="text/css" th:href="@{/common/jquery-ui-1.11.4/jquery-ui.min.css}" />

<!-- jquery组件引用 -->
<script th:src="@{/common/jquery/jquery-1.12.4.min.js}"></script>

<script type="text/javascript">
	// 项目路径
	/* var ctx = "${pageContext.request.contextPath}"; */
	var ctx = "/projectName";
</script>


</head>

其中引入的静态资源在resources/static/common/jquery目录下,head.html在resources/templates目录下

2.在其他页面中使用上面所创建的head.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="head :: commonHead('验证公共静态资源的引入')"></head>
<body>
    <h1>Thymeleaf模板文件</h1>
</body>
</html>

其中<head th:replace="head :: commonHead('验证js')"></head>中的head对应head.html的名字,commonHead对应head.html中的th:fragment="commonHead(title)"

 

默认页面

在没有设置默认页面,在地址栏输入http://ip:端口号(eg:localhost:8080,springboot项目默认访问路径为"/",访问时直接使用http://ip:端口号,但是可以指定访问路径,如果指定了访问路径,访问时要带上访问路径:http://ip:端口号/访问路径,eg:localhost:8080/demo)时访问页面出错,下面设置默认页面

方法一:在controller中写一个类,如下:

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(Model model, HttpServletResponse response) {
        model.addAttribute("name", "simonsfan");
        return "index";
    }
}

方法二:新建一个DefaultViewController类,如下:

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class DefaultViewController extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
}

模板页面实时生效

1、禁用模板引擎的缓存:spring.thymeleaf.cache=false

2、页面修改后ctrl+F9

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值