zoukankan      html  css  js  c++  java
  • 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>

      例子中有两点需要注意的地方:

    1. ${param.q!=null}检查set中是否有参数q
    2. 参数是一个数组,因为它可以多值比如?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();
    }

    转自:http://www.cnblogs.com/jiangchao226/p/5938152.html

  • 相关阅读:
    辨析六种单例模式
    理解模板模式
    常用DOM API总结
    自我剖析——一天的效率
    对两个数组合并,并去重
    Qt 创建一个QtDesinger第三方控件
    Qt中QComboBox中自定义界面使用stylesheet实现下拉按钮独立效果
    内核对象 windows操作系统
    python之基本内容
    python之总体理解
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/thymeleafModel.html
Copyright © 2011-2022 走看看