Thymeleaf+SpringMVC,如何从模板中获取数据

在一个典型的SpringMVC应用中,带@Controller注解的类负责准备数据模型Map的数据和选择一个视图进行渲染。这个模型Map对视图进行完全的抽象,在使用Thymeleaf的情况下,它将是一个VariablesMap对象(即Thymeleaf模板执行上下文的属性),使其可以用于模板重点表达式。

Spring中Model的attributes属性

SpringMVC调用可以在视图模型的执行过程中访问的数据,在Thymeleaf中相当于上下文变量。

在SpringMVC中添加一个attributes有几种不同的方法,下面有一些常见的情况:

ModeladdAttribut方法新增一个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();
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring MVC,你可以使用@RequestParam注解来获取表单数据。同时,如果你使用Thymeleaf作为模板引擎,你可以在表单使用Thymeleaf的语法来绑定表单数据。 首先,确保你已经在你的Spring MVC配置配置了Thymeleaf视图解析器。例如,在你的`application.properties`或`application.yml`添加以下配置: ``` spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 接下来,在你的Controller,使用@RequestParam注解来获取表单数据。例如,假设你有一个表单提交了一个名为"name"的字段,你可以这样获取它的值: ```java @PostMapping("/submit") public String handleSubmit(@RequestParam("name") String name) { // 处理表单数据 return "redirect:/success"; } ``` 在上面的例子,`@RequestParam("name")`表示获取名为"name"的表单字段的值,并将其赋值给`name`变量。 然后,在你的Thymeleaf模板,你可以使用Thymeleaf的语法来绑定表单数据。例如,如果你想在表单显示之前提交的名字,可以使用以下代码: ```html <form th:action="@{/submit}" method="post"> <input type="text" name="name" th:value="${name}" /> <button type="submit">Submit</button> </form> ``` 在上面的例子,`${name}`表示从Controller传递过来的名为"name"的属性值。 这样,你就可以在Spring MVC获取和使用Thymeleaf表单数据了。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值