Apache Tiles:在运行时更改模板页面

发布于 2024-09-28 16:31:52 字数 300 浏览 2 评论 0原文

我有一个问题:假设在 Spring MVC 3.0 环境中我使用 Tiles 管理视图:我有一个包含所有视图定义的 xml 文件。每个视图都扩展一个特定的模板。我有两个模板:一个用于渲染completeDOM(),一个用于partialDOM(.....)。问题是,有一些视图可以在fullDOM和partialDOM中检索,但我不想写出两个相似的定义。

我正在考虑一种动态方法:在运行时注入视图的模板,指定一个应包含模板名称的 http 参数。如果请求包含该参数,则 Tiles 应该使用 http 参数值检测到的模板来覆盖视图扩展的模板。

一些建议?

I have a question : suppose that in a spring MVC 3.0 enviroment i manage Views with Tiles : I have a xml file with definitions of all views. Every view extends a specific template. I have two templates : one for rendering a completeDOM () and one for partialDOM (.....).The problem is, there are some views that can be retrieved in fullDOM and also in partialDOM, but i don't want to write two similars definitions.

I was thinking to a dynamic approach : inject the template of a view at runtime, specifying an http parameter which should contains the name of the template. If the request contains the parameter, than Tiles should override the template exteded by the view, with the template detected by http parameter value.

Some suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轮廓§ 2024-10-05 16:31:52

我知道这是一个老问题,但我需要做这件事,所以我想我会分享我的解决方案。

Tiles 允许他们所谓的“运行时组合”,它可以让您修改定义。因此,您可以重用现有定义并只需交换模板:

<tiles:insertDefinition name="existingDefinition" template="alternateTemplate.jsp" />

I know this is an old question but I had needed to do this very thing so I thought I'd share my solution.

Tiles allows what they refer to as "runtime composition", which lets you modify definitions. So you can reuse an existing definition and just swap the template:

<tiles:insertDefinition name="existingDefinition" template="alternateTemplate.jsp" />
水晶透心 2024-10-05 16:31:52

在 springtilesConfigurer 中,您需要设置可变容器:

<property name="useMutableTilesContainer" value="true"/>
<property name="checkRefresh" value="true"/>

在 Spring 控制器中:

ModelAndView model = new ModelAndView();
MutableTilesContainer container = (MutableTilesContainer)ServletUtil.getContainer(request.getSession().getServletContext());
Attribute attribute = new Attribute("your template jsp");
HashMap<String, Attribute>  attributes = new HashMap<String, Attribute>();
attributes.put("body", attribute);
Definition definition = new Definition("your definition name", "your jsp", attributes);
definition.setExtends("your definition template name");
definition = PatternUtil.replacePlaceholders(definition, "your definition name", new Object());
container.register(definition, request, response);
model.setViewName("your definition name");

In spring tilesConfigurer you need to set mutable container:

<property name="useMutableTilesContainer" value="true"/>
<property name="checkRefresh" value="true"/>

And in your Spring Controller:

ModelAndView model = new ModelAndView();
MutableTilesContainer container = (MutableTilesContainer)ServletUtil.getContainer(request.getSession().getServletContext());
Attribute attribute = new Attribute("your template jsp");
HashMap<String, Attribute>  attributes = new HashMap<String, Attribute>();
attributes.put("body", attribute);
Definition definition = new Definition("your definition name", "your jsp", attributes);
definition.setExtends("your definition template name");
definition = PatternUtil.replacePlaceholders(definition, "your definition name", new Object());
container.register(definition, request, response);
model.setViewName("your definition name");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文