Thymeleaf+SpringMVC,如何从模板中获取数据
Thymeleaf+SpringMVC,如何从模板中获取数据
在一个典型的SpringMVC应用中,带@Controller注解的类负责准备数据模型Map的数据和选择一个视图进行渲染。这个模型Map对视图进行完全的抽象,在使用Thymeleaf的情况下,它将是一个VariablesMap对象(即Thymeleaf模板执行上下文的属性),使其可以用于模板重点表达式。
Spring中Model的attributes属性
SpringMVC调用可以在视图模型的执行过程中访问的数据,在Thymeleaf中相当于上下文变量。
在SpringMVC中添加一个attributes有几种不同的方法,下面有一些常见的情况:
给Model的addAttribut方法新增一个attribute
@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("messages", messageRepository.findAll());
return "message/list";
}
在ModelAndView的返回值中添加:
@RequestMapping(value = "message", method = RequestMethod.GET)
public ModelAndView messages() {
ModelAndView mav = new ModelAndView("message/list");
mav.addObject("messages", messageRepository.findAll());
return mav;
}
通过@ModelAttribute注解暴露出公告方法:
@ModelAttribute("messages")
public List<Message> messages() {
return messageRepository.findAll();
}
你可能已经注意到了,在上述的将messages属性添加到model的方法中,Thymeleaf视图均可用。
在Thymeleaf中,这些model的attributes属性值均可以使用${attributeName}来访问,这个attributeName对于Thymeleaf来说就是一个messages,这是一个SpringEL表达式,总之,SpringEL表达式是一种支持在运行时查询和操作对象图的语言。
在Thymeleaf中访问model的Attributes方式如下:
<tr th:each="message : ${messages}">
<td th:text="${message.id}">1</td>
<td><a href="#" th:text="${message.title}">Title ...</a></td>
<td th:text="${message.text}">Text ...</td>
</tr>
Request参数
Request参数在Thymeleaf视图中可以很容易的使用,Request参数一般为从客户端到服务器传送参数,如:
https://example.com/query?q=Thymeleaf+Is+Great!
现在假设有一个@Controller控制器,控制器中重定向的方式发送一个request参数:
@Controller
public class SomeController {
@RequestMapping("/")
public String redirect() {
return "redirect:/query?q=Thymeleaf Is Great!";
}
}
访问参数q可以使用param前缀
<p th:text="${param.q[0]}" th:unless="${param.q == null}">Test</p>
例子中有两点需要注意的地方:
- ${param.q!=null}检查set中是否有参数q
- 参数是一个数组,因为它可以多值比如?q=a&r=b
还有一种访问方式是使用#httpServletRequest对象,可以直接进入javax.servlet.http.HttpServletRequest对象:
<p th:text="${#httpServletRequest.getParameter('q')}" th:unless="${#httpServletRequest.getParameter('q') == null}">Test</p>
Session属性
比如为session添加了一个mySessionAttribute属性:
@RequestMapping({"/"})
String index(HttpSession session) {
session.setAttribute("mySessionAttribute", "someValue");
return "index";
}
和Request参数访问方式类似,这里使用session前缀:
<div th:text="${session.mySessionAttribute}">[...]</div>
同样的,还可以使用#httpSession方式访问,它直接进入javax.servlet.http.HttpSession对象。
ServletContext属性
ServletContext属性可以再request和session中共享,未来访问ServletContext属性,可以使用application前缀:
<table>
<tr>
<td>context中的attribute</td>
<!-- 检索ServletContext的属性'myContextAttribute' -->
<td th:text="${application.myContextAttribute}">42</td>
</tr>
<tr>
<td>attributes数量:</td>
<!-- 返回attributes的数量 -->
<td th:text="${application.size()}">42</td>
</tr>
<tr th:each="attr : ${application.keySet()}">
<td th:text="${attr}">javax.servlet.context.tempdir</td>
<td th:text="${application.get(attr)}">/tmp</td>
</tr>
</table>
Spring beans
Thymeleaf可以通过@beanName访问Spring应用上下午中注册的bean,如
<div th:text="${@urlService.getApplicationUrl()}">...</div>
在这个例子中,@urlService就是在上下文中注册的Spring Bean:
@Configuration
public class MyConfiguration {
@Bean(name = "urlService")
public UrlService urlService() {
return new FixedUrlService("somedomain.com/myapp"); // 一个实现
}
}
public interface UrlService {
String getApplicationUrl();
}
Thymeleaf+SpringMVC,如何从模板中获取数据的更多相关文章
- SpringMVC从Request域中获取数据
SpringMVC从Request域中获取数据的三种方式 SpringMVC环境自行搭建, 约定存在如下目录和文件:/WEB-INF/pages/success.jsp 方式一:传入Model对象 前 ...
- Thymeleaf+SpringMVC,如何从模板中获取数据(转)
在一个典型的SpringMVC应用中,带@Controller注解的类负责准备数据模型Map的数据和选择一个视图进行渲染.这个模型Map对视图进行完全的抽象,在使用Thymeleaf的情况下,它将是一 ...
- POI往word模板中写入数据
转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
- django的html模板中获取字典的值
在django的html模板中获取字典中的值应当直接使用 字典.[key] 的方式 {% for i in lists %} <li id="{{i.id}}" class ...
- hive从查询中获取数据插入到表或动态分区
Hive的insert语句能够从查询语句中获取数据,并同时将数据Load到目标表中.现在假定有一个已有数据的表staged_employees(雇员信息全量表),所属国家cnty和所属州st是该表的两 ...
- 哪种方式更适合在React中获取数据?
作者:Dmitri Pavlutin 译者:小维FE 原文:dmitripavlutin.com 国外文章,笔者采用意译的方式,以保证文章的可读性. 当执行像数据获取这样的I/O操作时,你必须发起获取 ...
- Django Form 实时从数据库中获取数据
修改 models.py 添加 class UserType(models.Model): caption = models.CharField(max_length=32) 执行命令,生成数据库 p ...
- SpringMVC从Session域中获取值
SpringMVC从Session域中获取值 SpringMVC环境自行搭建 第一步:前端页面 第二步.后台代码 第三步.响应视图 第四步.在当前处理器所在的类设置@SessionAttributes ...
- SQL语句的使用,SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中删除数据 INSERT INTO - 向数据库表中插入数据
SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法. 但是 SQL 语言也包含用于更新. ...
随机推荐
- 系统建设 > 医疗集团CRM系统建设步骤与分析
概述 医院客户关系管理系统(Customer Relationship Management,简称CRM)是一个完善的“以病人为中心”的管理系统,为集团/医院/总院分院/管理机构提供院前.院中.院后的 ...
- angular源码阅读的起点,setupModuleLoader方法
angular源码其实结构非常清晰,划分的有条有理的,大概就是这样子: (function(window,document,jquery,undefined){ //一些工具函数 //EXPR 编译器 ...
- ListView 刷新加载控件
1.MaterialRefreshLayout刷新加载: 导入依赖: compile 'com.cjj.materialrefeshlayout:library:1.3.0' 布局 <com.c ...
- Magnifier笔记
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- JDBC三层架构
三层框架: 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL).区分层次的目的即为了“高内聚,低耦合”的思想. 原理:1:数据访问层:主要是对原始 ...
- Using GET_APPLICATION_PROPERTY in Oracle D2k Forms
Using GET_APPLICATION_PROPERTY in Oracle D2k Forms DescriptionReturns information about the current ...
- NSJSONSerialization(json序列化)
//通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据 NSJSONSerialization isValidJSONObject:obj 我们能利用N ...
- 一种更清晰的Android架构(转)
一种更清晰的Android架构 一种更清晰的Android架构 原文链接 : Architecting Android…The clean way? 译者 : Mr.Simple & So ...
- 发布一个开源极致的javascript模板引擎tpl.js
tpl.js(大家直接去https://git.oschina.net/tianqiq/tpl.js这个上面看) 简介 tpl.js是一个比较极致(极小,极快,极简单)的js模板引擎,可以在各种js环 ...
- iOS - AppRealTest App 真机测试
前言 1.准备 开发者账号 自从 Xcode7 出来之后,一般的真机测试不需要开发者账号,也就不需要看这篇教程,只有 app 具有 "推送" 等功能的时候,要真机测试就必须要开发者 ...