SpringBoot(二)

简介: SpringBoot(二)

FreeMarker常用指令(遍历List集合)


这里使用.ftlh渲染出来的界面,属性如果为空值,则显示不出来。

需要在ftlh中引入这个指令。


<#setting classic_compatible=true>


Controller:

 //查询全部员工信息,展示
    @RequestMapping("/showEmp")
    public ModelAndView testList(){
        ModelAndView mv=new ModelAndView();
        List<Emp> list=empService.findAll();
        System.out.println(list.toString());
        mv.addObject("empList",list);
        mv.setViewName("showEmp");
        return mv;
    }

showEmp.ftlh:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        #empTable {
            width: 80%;
            border: 1px solid blue;
            margin: 0px auto;
        }
        #empTable th, td {
            border: 1px solid green;
            text-align: center;
        }
    </style>
</head>
<body>
<table id="empTable" cellpadding="0px" cellspacing="0px">
    <tr>
        <th>索引</th>
        <th>工号</th>
        <th>姓名</th>
        <th>岗位</th>
        <th>薪资</th>
        <th>部门号</th>
    </tr>
    <#list empList as emp>
        <#setting classic_compatible=true>
    <tr>
<#--        //通过_index可以得到循环的下标-->
        <td>${emp_index}</td>
        <td></td>
        <td>${emp.ename}</td>
        <td>${emp.job}</td>
        <td>${emp.sal}</td>
        <td>${emp.deptno}</td>
    </tr>
    </#list>
</table>
</body>
</html>

FreeMarker遍历Map集合

Controller:

//查询全部员工信息,展示
    @RequestMapping("/showEmpMap")
    public ModelAndView testMap(){
        ModelAndView mv=new ModelAndView();
        List<Emp> list=empService.findAll();
        Map<String,Emp> empMap=new HashMap<>();
        for (Emp emp : list) {
            empMap.put(emp.getEmpno().toString(),emp);
        }
        mv.addObject("empMap",empMap);
        mv.setViewName("showEmpMap");
        return mv;
    }

showEmpMap.ftlh:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        #empTable {
            width: 80%;
            border: 1px solid blue;
            margin: 0px auto;
        }
        #empTable th, td {
            border: 1px solid green;
            text-align: center;
        }
    </style>
</head>
<body>
<#setting classic_compatible=true>
<#--查看Map集合中单独一个元素-->
<table id="empTable" cellpadding="0px" cellspacing="0px">
    <tr>
        <th>索引</th>
        <th>工号</th>
        <th>姓名</th>
        <th>岗位</th>
        <th>薪资</th>
        <th>部门号</th>
    </tr>
    <#list empMap?keys as k>
        <tr>
            <td>${k_index}</td>
            <td>${k}</td>
            <td>${empMap[k].ename}</td>
            <td>${empMap[k].job}</td>
            <td>${empMap[k].sal}</td>
            <td>${empMap[k].deptno}</td>
        </tr>
    </#list>
</table>
</body>
</html>

FreeMarker在遍历map集合是,key必须是String

FreeMaker内置函数:


内建函数语法格式: 变量+?+函数名称


显示年月日:         ${today?date} 
显示时分秒:      ${today?time} 
显示日期+时间:${today?datetime} 
自定义格式化:   ${today?string("yyyy年MM月")} 

SpringBoot整合Thymeleaf

导入依赖:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.4.5</version>
        </dependency>

showEmp.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        #empTable {
            width: 80%;
            border: 1px solid blue;
            margin: 0px auto;
        }
        #empTable th, td {
            border: 1px solid green;
            text-align: center;
        }
    </style>
</head>
<body>
展示单个员工信息
工号:<span th:text="${emp.empno}"></span><br>
姓名:<span th:text="${emp.ename}"></span><br>
职务:<span th:text="${emp.job}"></span><br>
上级:<span th:text="${emp.mgr}"></span><br>
入职日期:<span th:text="${emp.hiredate}"></span><br>
<hr/>
<table id="empTable" cellpadding="0px" cellspacing="0px" >
    <tr>
        <th>工号</th>
        <th>姓名</th>
        <th>职务</th>
        <th>上级</th>
        <th>入职日期</th>
        <th>工资</th>
        <th>补助</th>
        <th>部门</th>
        <th>操作</th>
    </tr>
    <tr th:each="emp:${empList}">
        <td th:text="${emp.empno}"></td>
        <td th:text="${emp.ename}"></td>
        <td th:text="${emp.job}"></td>
        <td th:text="${emp.mgr}"></td>
        <td th:text="${emp.hiredate}"></td>
        <td th:text="${emp.sal}"></td>
        <td th:text="${emp.comm}"></td>
        <td th:text="${emp.deptno}"></td>
        <td>
            <a th:href="@{removeEmp(empno=${emp.empno},ename=${emp.ename})}" >删除</a>
            <a href="javascript:void(0)" th:onclick="removeEmp([[${emp.empno}]],[[${emp.ename}]])">删除</a>
            <a href="javascript:void(0)" th:onclick="removeEmp([[${emp.empno}]],[[${emp.ename}]])">删除</a>
        </td>
    </tr>
</table>
<script>
    function removeEmp(empno,ename){
       var resulet= confirm("确定要删除编号为“+empno+”的"+ename);
        if(resulet){
            window.location.href="removeEmp?empno="+empno+"&ename="+ename;
        }
    }
</script>
</body>
</html>

Thymeleaf内置对象


Thymeleaf提供了一些内置对象,内置对象可直接在模板中使用。这些对象是以#引用的。

使用内置对象的语法

1引用内置对象需要使用#

2大部分内置对象的名称都以s结尾。如:strings、numbers、dates

3常见内置对象如下


#arrays:数组操作的工具;

#aggregates:操作数组或集合的工具;

#bools:判断boolean类型的工具;

#calendars:类似于#dates,但是是java.util.Calendar类的方法;

#ctx:上下文对象,可以从中获取所有的thymeleaf内置对象;

#dates:日期格式化内置对象,具体方法可以参照java.util.Date;

#numbers: 数字格式化;#strings:字符串格式化,具体方法可以参照String,如startsWith、contains等;

#objects:参照java.lang.Object;

#lists:列表操作的工具,参照java.util.List;

#sets:Set操作工具,参照java.util.Set;#maps:Map操作工具,参照java.util.Map;

#messages:操作消息的工具。


<span th:text="${#httpServletRequest.getAttribute('msg')}"></span><br/>
<span th:text="${#request.getAttribute('msg')}"></span><br/>
<span th:text="${msg}"></span><br/>
session:<br/>
<span th:text="${#httpSession.getAttribute('msg')}"></span><br/>
<span th:text="${#session.getAttribute('msg')}"></span><br/>
<span th:text="${session.msg}"></span><br/>
application:<br/>
<span th:text="${#servletContext.getAttribute('msg')}"></span><br/>
<span th:text="${application.msg}"></span><br/>

SpringBoot热部署

热部署之后则不需要重启项目,当监听到内容改变后tomcat则自动的进行重启。

步骤:

添加依赖:


<!--        开发者工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.4.5</version>
            <optional>true</optional>
        </dependency>

修改idea自动编译配置:

fb32c860c87e4df298b411f46bfbf6c8.png

04fe68b652ec436195ee54ff187d7891.png

这里要打勾。

注意在2022以前的版本需要修改Reigstry

Ctrl+Shift+Alt+/ 点击弹出框中Registry…


19d1775d7aad4073b8a892f706cc8cd0.png


4be349ea63654d74a5ddf472ea567731.png


相关文章
【微信公众平台对接】有关【上传图文消息内的图片获取URL】调用示例
【微信公众平台对接】有关【上传图文消息内的图片获取URL】调用示例
1032 0
|
人工智能 算法 决策智能
【五子棋实战】第2章 博弈树负值极大alpha-beta剪枝算法(一)
市面上比较常用的五子棋算法是博弈树极大极小值alpha-beta剪枝算法,该算法可以分成四个部分来讲解,它们是环环相扣的:博弈树 - 极大极小值搜索 - 负值极大法 - alpha&beta剪枝 。
1813 0
|
Ubuntu 数据可视化 Linux
Ubuntu卸载软件:3种卸载方式
只卸载程序。如果你移除程序但保留配置文件,请输入以下命令: sudo apt-get remove <programname>
|
前端开发 数据可视化 Java
SpringBoot的4中常见入参形式错误解析
在使用SpringBoot进行前后端接口对接时,常遇到如500、400等请求错误,本文总结了四个常见的复杂请求类型及其解决方案,包括实体嵌套List提交、普通文件上传、List提交及数组Array提交,详细展示了正确的前端与后端代码实现,帮助开发者避免常见错误,提高开发效率。
509 1
SpringBoot的4中常见入参形式错误解析
|
缓存 Java Spring
IDEA中配置HTML和Thymeleaf热部署的步骤
以上就是在IntelliJ IDEA中配置HTML和Thymeleaf热部署的步骤。这样的配置可以大大提高你的开发效率,让你更专注于代码的编写,而不是等待应用的重启。希望这个指南对你有所帮助!
1284 21
|
人工智能 调度
【MCP教程系列】在阿里云百炼上用Qwen3+且慢MCP,用AI重新定义资产管理效率
通义千问Qwen3通过MCP协议,在Agent中具备强大的工具调度与复杂任务拆解能力,成为构建复杂AI应用的核心引擎。以“基金助手”为例,集成且慢MCP服务后,可一键调用多源金融数据并动态组合分析工具,实现精准调度。在阿里云百炼平台上,只需4步即可构建一个“金融顾问”智能体:开通且慢MCP服务、新建智能体、添加MCP技能、测试效果。此外,还可增加提示词规范输出内容,完成更复杂的任务。
2748 0
|
Java 关系型数据库 MySQL
Maven——创建 Spring Boot项目
Maven 是一个项目管理工具,通过配置 `pom.xml` 文件自动获取所需的 jar 包,简化了项目的构建和管理过程。其核心功能包括项目构建和依赖管理,支持创建、编译、测试、打包和发布项目。Maven 仓库分为本地仓库和远程仓库,远程仓库包括中央仓库、私服和其他公共库。此外,文档还介绍了如何创建第一个 SpringBoot 项目并实现简单的 HTTP 请求响应。
1451 1
Maven——创建 Spring Boot项目
|
Java 应用服务中间件 Maven
解决“Unable to start embedded Tomcat“错误的完整指南
通过逐步检查以上问题,你应该能够解决 "Unable to start embedded Tomcat" 错误,并使Tomcat成功启动。
5394 1
解决“Unable to start embedded Tomcat“错误的完整指南
|
前端开发 JavaScript Java
Springboot图片上传和映射
Springboot图片上传和映射
711 0
|
网络协议 Shell Nacos
nacos简介安装配置创建
nacos简介安装配置创建
1387 1