Thymeleaf获取Map<>中的数据
时间: 2024-02-06 13:08:54 浏览: 31
可以使用 Thymeleaf 的表达式语言(Expression Language,简称为EL)来获取 Map 类型中的数据。
假设有以下的 Map 类型数据:
```
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
```
可以在 Thymeleaf 中使用 `${map.key1}` 或 `${map['key1']}` 表达式来获取 key1 对应的 value 值。
例如:
```
<p>key1 对应的值为: ${map.key1}</p>
<p>key2 对应的值为: ${map['key2']}</p>
```
输出结果为:
```
key1 对应的值为: value1
key2 对应的值为: value2
```
相关问题
用mybayis整合thymeleaf怎么进行分页查询
要使用 MyBatis 和 Thymeleaf 进行分页查询,需要进行以下步骤:
1. 首先,在 Mapper.xml 文件中编写 SQL 语句,使用 LIMIT 关键字进行分页查询。例如,查询从第 10 条开始的 5 条数据:
```
<select id="selectByPage" parameterType="map" resultMap="BaseResultMap">
select * from my_table
order by id desc
limit #{start}, #{pageSize}
</select>
```
其中,#{start} 表示从第几条数据开始查询,#{pageSize} 表示每页查询的数据条数。
2. 在 Service 层中编写分页查询的方法。方法中需要调用 MyBatis 的分页插件 PageHelper 来实现分页功能:
```
@Service
public class MyService {
@Autowired
private MyMapper myMapper;
public List<MyObject> selectByPage(int pageNum, int pageSize) {
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
List<MyObject> myObjects = myMapper.selectByPage();
// 使用 PageInfo 对象获取分页信息
PageInfo<MyObject> pageInfo = new PageInfo<>(myObjects);
return myObjects;
}
}
```
3. 在 Controller 层中获取前端传递的 pageNum 和 pageSize 参数,并调用 Service 层中的分页查询方法:
```
@Controller
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/list")
public String list(Model model,
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "5") int pageSize) {
List<MyObject> myObjects = myService.selectByPage(pageNum, pageSize);
model.addAttribute("myObjects", myObjects);
return "list";
}
}
```
4. 在 Thymeleaf 模板中使用 PageHelper 对象获取分页信息,并展示分页导航条和数据列表:
```
<div th:if="${#lists.size(myObjects) == 0}">暂无数据</div>
<div th:each="myObject : ${myObjects}">
<p th:text="${myObject.name}"></p>
</div>
<div th:if="${#lists.size(myObjects) > 0}">
<ul class="pagination">
<li th:class="${pageInfo.isFirstPage() ? 'disabled' : ''}">
<a th:href="@{/list(pageNum=1,pageSize=${pageSize})}" aria-label="First">
<span aria-hidden="true">«</span>
</a>
</li>
<li th:class="${pageInfo.hasPreviousPage() ? '' : 'disabled'}">
<a th:href="@{/list(pageNum=${pageInfo.prePage},pageSize=${pageSize})}" aria-label="Previous">
<span aria-hidden="true">‹</span>
</a>
</li>
<li th:each="num : ${pageInfo.navigatepageNums}"
th:class="${num == pageInfo.pageNum ? 'active' : ''}">
<a th:href="@{/list(pageNum=${num},pageSize=${pageSize})}" th:text="${num}"></a>
</li>
<li th:class="${pageInfo.hasNextPage() ? '' : 'disabled'}">
<a th:href="@{/list(pageNum=${pageInfo.nextPage},pageSize=${pageSize})}" aria-label="Next">
<span aria-hidden="true">›</span>
</a>
</li>
<li th:class="${pageInfo.isLastPage() ? 'disabled' : ''}">
<a th:href="@{/list(pageNum=${pageInfo.pages},pageSize=${pageSize})}" aria-label="Last">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</div>
```
其中,PageInfo 对象可以通过在 Service 层中使用 PageHelper.startPage() 方法后获取到。Thymeleaf 中的 #lists.size() 方法可以获取列表的大小,用于判断是否有数据需要展示。分页导航条可以使用 Bootstrap 等框架进行美化。
springboot+thymeleaf网页如何同时显示两个数据库表
你可以使用Spring Data JPA来访问两个数据库表,然后在Thymeleaf模板中分别展示它们的数据。
首先,在你的Spring Boot应用中,你需要配置两个数据源和实体管理器工厂。这个可以通过在application.properties文件中添加以下配置来完成:
```
# 第一个数据源
spring.datasource.url=jdbc:mysql://localhost:3306/db1
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 第二个数据源
datasource2.url=jdbc:mysql://localhost:3306/db2
datasource2.username=root
datasource2.password=password
datasource2.driver-class-name=com.mysql.jdbc.Driver
# 配置JPA
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
```
然后,你需要在你的应用中定义两个数据源的JdbcTemplate实例,以便你可以使用它们来执行查询。这可以通过像这样在你的应用中定义两个JdbcTemplate实例来完成:
```
@Configuration
public class DataSourceConfig {
@Bean(name = "jdbcTemplate1")
public JdbcTemplate jdbcTemplate1(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "jdbcTemplate2")
public JdbcTemplate jdbcTemplate2(@Qualifier("dataSource2") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
```
接下来,你需要为每个数据库表定义一个实体类,并使用Spring Data JPA创建一个存储库来访问它们。例如,对于一个名为“table1”的表,你可以定义一个名为“Table1”的实体类和一个名为“Table1Repository”的存储库:
```
@Entity
@Table(name = "table1")
public class Table1 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
// 省略getter和setter
}
@Repository
public interface Table1Repository extends JpaRepository<Table1, Long> {
}
```
然后,在你的控制器中,你可以注入这两个存储库,并使用它们来获取两个数据库表的数据。例如:
```
@Controller
public class MyController {
@Autowired
private Table1Repository table1Repository;
@Autowired
@Qualifier("jdbcTemplate2")
private JdbcTemplate jdbcTemplate2;
@GetMapping("/")
public ModelAndView index() {
List<Table1> table1List = table1Repository.findAll();
List<Map<String, Object>> table2List = jdbcTemplate2.queryForList("SELECT * FROM table2");
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("table1List", table1List);
modelAndView.addObject("table2List", table2List);
return modelAndView;
}
}
```
最后,在Thymeleaf模板中,你可以使用类似以下的代码来展示这两个数据库表的数据:
```
<!-- 展示第一个数据库表的数据 -->
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr th:each="table1 : ${table1List}">
<td th:text="${table1.id}"></td>
<td th:text="${table1.name}"></td>
</tr>
</tbody>
</table>
<!-- 展示第二个数据库表的数据 -->
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
</thead>
<tbody>
<tr th:each="table2 : ${table2List}">
<td th:text="${table2.id}"></td>
<td th:text="${table2.name}"></td>
<td th:text="${table2.age}"></td>
</tr>
</tbody>
</table>
```