Thymeleaf 可以访问 Spring servletContext 吗? [英] Can Thymeleaf access the Spring servletContext?

查看:22
本文介绍了Thymeleaf 可以访问 Spring servletContext 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以提供帮助.我们正在将 Spring Webflow 2 应用程序从使用基于 jsp 的视图层转换为基于 Thymeleaf 的视图.

I wonder if anyone can help. We are in the process of converting a Spring Webflow 2 application from using a jsp based view layer, to a Thymeleaf based view.

在大多数情况下,这是可以的,但现在我正在努力让 Thymeleaf 访问我们放入 servletContext 中的对象.

For this most part this is OK, but now I'm struggling to get Thymeleaf to access an object that we've put in the servletContext.

因此,我们有一个对象作为 bean 的一部分放入 servletContext(实现 ServletContextAwareInitializingBean)

So, we have an object that is put in the servletContext as part of a bean (implementing ServletContextAware and InitializingBean)

为了简单起见,假设它是一个字符串:

For the sake of simplicity, lets say it is a string:

public class ReferenceDataBuilder implements ServletContextAware, InitializingBean {

public void setServletContext(ServletContext p_context) {
    p_context.setAttribute("referenceData", "test text" );
}

在基于jsp的视图中,我们可以像这样访问referenceData对象:

In our jsp based views, we can access the referenceData object like this:

<p><c:out value="${referenceData}"/></p>

借助 Spring EL 的魔力,它知道它可以访问的各种范围(servletContextflowScopeflashScope 等),并且(我猜?)搜索每个范围,直到找到匹配的属性.结果是:

By the magic of Spring EL, it knows the various scopes it has access to (servletContext, flowScope, flashScope etc), and (I'm guessing?) searches each scope until it finds a matching property. The result is that:

<p>test text</p>

在视图中呈现.

在我们的 thymeleaf 模板中,我们正在尝试做同样的事情:

In our thymeleaf template, we are trying to do the same thing:

<p th:text="${referenceData}"/></p>

但这只是返回一个空字符串.视图呈现一个空字符串:

But this simply returns an empty string. The view renders an empty string:

<p></p>

(但我认为 EL 实际上是作为空值返回的)

(but I think the EL is actually being returned as a null)

我很确定,如果 referenceData 对象是诸如 flowScopeflashScope 之类的范围的属性,这会起作用 - 但是它不是,它是 servletContext 的一个属性.

I'm pretty sure that if the referenceData object were a property of a scope such as flowScope or flashScope this would work - but its not, its a property of servletContext.

有谁知道 thymeleaf 是否可以通过 EL 访问 servletContext ?也许我需要使用不同的语法?

Does anyone know if thymeleaf can access the servletContext via EL? Perhaps theres a different syntax I need to use?

干杯

内森

推荐答案

您可以通过 #ctx 对象访问常用地图,该对象类型为 SpringWebContext.

You could access usual maps via the #ctx object, which is of type SpringWebContext.

例如,#ctx.locale、#ctx.httpServletRequest.contextPath、#ctx.servletContext 甚至 #ctx.applicationContext 对于 Spring applicationContext.

For example #ctx.locale, #ctx.httpServletRequest.contextPath, #ctx.servletContext or even #ctx.applicationContext for Spring applicationContext.

您可以使用直接方法调用

You could use direct method invocation

<p th:text="${#ctx.servletContext.getAttribute('referenceData')}">Whatever</p>

或 applicationAttributes 变量映射

or the applicationAttributes variables map

<p th:text="${#ctx.servletContext.applicationAttributes.referenceData}">Whatever</p>

或使用 Spring 隐式对象更简单的事件

or event simpler using Spring implicit object

<p th:text="${application.referenceData}">Whatever</p>

这篇关于Thymeleaf 可以访问 Spring servletContext 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆