Thymeleaf+SpringMVC,如何从模板中获取数据
来源:互联网 发布:软件开发工具是什么 编辑:程序博客网 时间:2024/06/05 12:47
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参数:
@Controllerpublic 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:
@Configurationpublic class MyConfiguration { @Bean(name = "urlService") public UrlService urlService() { return new FixedUrlService("somedomain.com/myapp"); // 一个实现 }}public interface UrlService { String getApplicationUrl();}
- Thymeleaf+SpringMVC,如何从模板中获取数据
- Thymeleaf+SpringMVC,如何从模板中获取数据
- Thymeleaf+SpringMVC,如何从模板中获取数据
- Thymeleaf+SpringMVC,从模板中获取数据session、request参数
- Thymeleaf+SpringMVC,从模板中获取数据session、request参数
- SpringBoot学习(三)从yml文件中获取数据和Thymeleaf 模板引擎
- 在SpringMVC 中使用 Thymeleaf 模板引擎
- SpringMVC整合Thymeleaf模板
- SpringMVC 配置Thymeleaf模板
- SpringMVC从Controller中获取json数据
- Thymeleaf+SpringMVC,在html中获取数据session、request参数的值
- Thymeleaf中each标签遍历list如何获取index
- springboot中使用thymeleaf模板
- EJS模板中,js如何获取后端传来的数据
- 关于使用tpl模板如何从模板中取数据放入js中
- 演示如何从DataSet中获取LOB数据
- WSS3SDK之:如何从列表中获取数据
- android app如何从数据库中获取需要的数据?
- 【hdu 1232】 畅通工程
- Nginx之简单使用及配置-yellowcong
- 基于TP-GAN的侧脸人像恢复
- 面试题5:从头到尾打印链表
- IP报文中的头部设置
- Thymeleaf+SpringMVC,如何从模板中获取数据
- 在vue中methods互相调用的方法
- SSH中Spring以及hibernate、Redis的xml配置
- fatal: destination path '.' already exists and is not an empty directory. 错误及解决办法
- MongoDB安装和配置WINDOWS服务(1.x.x版本无serviceName的问题)
- 多标签分类的学习感悟
- bzoj 3160: 万径人踪灭 manachar+快速傅里叶变换
- OpenCV创建滑动条函数:createTrackbar()介绍
- windows下通过SmartRF flash programmer 2烧写代码