以简单的例子从头开始建spring boot web多模块项目(五)-thymeleaf引擎

继续向里面加,这次是引入thymeleaf渲染引擎。
使用这个引擎的很多,主要是以下几个优点:

  1. Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎。
  2. Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 -HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。
  3. Thymeleaf拥有适用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力。
    步骤:
    1、pom.xml中引入包:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

同时确保spring-boot-starter-web这个包已经引入。

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

2、其实默认thymeleaf配置情况下已经可以用了,不过它默认启用了缓存,更适合生产环境,不太适合开发环境,需要关掉。
application.properties文件中,加入配置:

#thymeleaf 缓存设置
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.suffix=.html

主要注意的是上面都是默认配置,只有cache=false是修改的。
prefix=classpath:/templates/两边的/都不要漏掉。

3、创建/templates/目录,在resources下。
创建模板文件。
web_1.html 这个包含了一个最简单的text的变量绑定

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
<!--  <meta http-equiv="content-type" content="text/html;chatset=UTF-8">-->
    <title>Title</title>
</head>
<body>
<p th:text="${hello}"></p>
</body>
</html>

web_2.html 这个包含了一个List类型变量的绑定,主要是演示遍历循环

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
<!--  <meta http-equiv="content-type" content="text/html;chatset=UTF-8">-->
    <title>Title</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td>ID</td>
        <td>Fid</td>
        <td>Fname</td>
    </tr>
    <tr th:each="ware:${warehouses}">
        <td th:text="${ware.Id}"></td>
        <td th:text="${ware.Fid}"></td>
        <td th:text="${ware.Fname}"></td>
    </tr>
</table>
</body>
</html>

3、编写控制器文件 Demo2Controller.java

package org.rainpet.controller;

import org.rainpet.entity.Warehouse;
import org.rainpet.service.WarehouseService;
import org.rainpet.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("/demo2")
public class Demo2Controller {
    
    @Autowired
    WarehouseService warehouseService;

    @GetMapping("web1")
    public String hello(Model model){
        model.addAttribute("hello","hello welcome");
        return "web_1";
    }

    @GetMapping("web2")
    public String web2(HttpServletRequest request){
        String name="note";
        request.setAttribute("hello",name);
        return "web_1";
    }
    @GetMapping("web3")
    public ModelAndView web3(){
        String name="note3";
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("name",name);

        List<Warehouse> warehouseList=new LinkedList<>();
        Warehouse warehouse=new Warehouse();
        warehouse.setId(1);
        warehouse.setFid("01");
        warehouse.setFname("测试01");
        warehouseList.add(warehouse);
        modelAndView.addObject("warehouses",warehouseList);
        modelAndView.addObject("warehouse",warehouse);
        modelAndView.setViewName("web_2");
        return modelAndView;
    }
}

需要注意的问题:
① thymeleaf需要配合@Controller 注解使用,一定不能使用@RestController注解
② LinkedList 对象需要java7及以上支持,需要修改项目的java兼容性,我是修改到了java8。
需要修改:Project Structure中项目的SDK、language level;
在这里插入图片描述

各个模块的Language Level、Dependencies中的Model SDK;
在这里插入图片描述
在这里插入图片描述

设置中的Build.Execution,Deployment中Compiler中,java Compiler的各个模块字节码的version,均改为8。在这里插入图片描述

重新编译,同时在父项目中maven中执行install操作方可。
在这里插入图片描述
不然,可能会出现莫名错误:Could not resolve dependencies for project org.rainpet:Web:jar:1.0-SNAPSHOT: Failed to collect dependencies

4、重新编译,运行,打开页面:
http://localhost:8081/demo2/web1
在这里插入图片描述
http://localhost:8081/demo2/web2
在这里插入图片描述
http://localhost:8081/demo2/web3
在这里插入图片描述
到此,项目已经跑起来了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwprain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值