Thymeleaf 模板引擎入门案例 结合 SpringBoot框架(二)

Thymeleaf 模板引擎入门案例 结合 SpringBoot框架(二)

  • 预期效果:点击侧边栏哪个选项时,该选项高亮

  • 做法:引入片段的时候传入参数
    th:class="${activeUri == 'main.html'? 'nav-link active':'nav-link'}"
<a class="nav-link active"
                   th:class="${activeUri == 'main.html'? 'nav-link active':'nav-link'}"
                   href="#" th:href="@{/main.html}">
  • 页面引入片段时需要传参
<!--引入sidebar-->
				<div th:replace="commons/bar::#sidebar(activeUri='main.html')"></div>

需要特别注意:这种写法是不对的! 

<!--引入侧边栏-->
				<div th:replace="~{commons/bar::#sidebar(activeUri='emps')}"></div>

应改为 => 

th:replace="commons/bar::#sidebar(activeUri='emps')"
<!--引入侧边栏-->
				<div th:replace="commons/bar::#sidebar(activeUri='emps')"></div>

 

  • list.html 前端页面遍历 后端controller传递的数据Collection
  • 注:这里的传参 ModelAndView使用错误,需要进行修改

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao ed;

    //查询所有员工返回列表页面
    @GetMapping("/emps")
    public String list(ModelAndView mv) {
        Collection<Employee> employees = ed.getAll();
        mv.addObject("emps", employees);
        //thymeleaf默认会拼串 classpath:/templates/ .html
        return "emp/list";
    }
}
  • SpringMVC响应数据和结果视图
  • Map,Model,ModelMap 隐形参数的作用和关系(重点)
    使用Map传递页面数据
    使用Model传递页面数据
    使用ModelMap传递页面数据
    总结:以上三个隐形参数传过来的是同一个实现类对象:BindingAwareModelMap,底层都会把键值对数据放到
    request作用域中.

  • @GetMapping("/emps")
        public String list(Model model) {
            Collection<Employee> employees = ed.getAll();
            System.out.println(employees.size());
            model.addAttribute("emps", employees);
            //thymeleaf默认会拼串 classpath:/templates/ .html
            return "emp/list";
        }

    list.html页面 使用 thymeleaf模板遍历结果集:

  • <table class="table table-striped table-sm">
    							<thead>
    								<tr>
    									<th>#</th>
    									<th>lastName</th>
    									<th>email</th>
    									<th>gender</th>
    									<th>department</th>
    									<th>birth</th>
    								</tr>
    							</thead>
    							<tbody>
    								<!--遍历emps,取出的每个变量为emp-->
    								<tr th:each="emp:${emps}">
    									<td th:text="${emp.id}"></td>
    									<td>[[${emp.lastName}]]</td>
    									<td th:text="${emp.email}"></td>
    									<td th:text="${emp.gender}==0?'女':'男'"></td>
    									<td th:text="${emp.department.departmentName}"></td>
    									<td th:text="${emp.birth}"></td>
    								</tr>
    							</tbody>
    						</table>

    页面效果

 补充:使用thymeleaf修改页面日期格式显示

<td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>

 页面效果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中使用Thymeleaf模板引擎访问静态HTML的过程如下: 1. 在Spring Boot工程中创建一个静态HTML文件,例如index.html。 2. 在application.properties文件中添加以下配置: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 这个配置告诉Thymeleaf模板引擎,它应该在classpath:/templates/目录下查找HTML模板文件,后缀为.html。 3. 创建一个Controller,用于处理请求并将数据传递给模板: ```java @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, world!"); return "index"; } } ``` 这个Controller处理根路径的GET请求,并将一个名为message的属性添加到Model中。然后,它返回index作为视图的名称。 4. 在index.html中使用Thymeleaf模板引擎来呈现message属性: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 这个HTML文件使用Thymeleaf的th:text属性来呈现message属性。 5. 启动Spring Boot应用程序,并访问http://localhost:8080/,应该看到Hello, world!。 注意:在上述步骤中,我们将HTML文件放在了classpath:/templates/目录下,这是因为Thymeleaf默认会在这个目录下查找模板文件。如果你想将HTML文件放在其他地方,可以在application.properties文件中通过配置spring.thymeleaf.prefix属性来指定模板文件的路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值