如何更改 Apache Tiles 中图块评估的顺序?

发布于 2024-10-03 14:50:41 字数 1272 浏览 5 评论 0原文

我遇到一个问题,嵌套模板在父模板之前被评估。由于排序问题,这会导致坚持嵌套的标签库出现问题。

template.jsp

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://taglibs.com/tags" prefix="s"%>
<%System.out.println("evaluating template.jsp");%>
<s:outer>
    <tiles:insertAttribute name="content" />
</s:outer>

content.jsp

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://taglibs.com/tags" prefix="s"%>
<tiles:insertDefinition name="template">
    <tiles:putAttribute name="content" />
        <%System.out.println("evaluating content.jsp");%>
        <s:inner />
    </tiles:putAttribute>
</tiles:insertDefinition>

当评估 content.jsp 页面时,将输出以下内容。

evaluating content.jsp
evaluating template.jsp

content.jsp 页面失败,因为 inner 标记必须位于 outer 标记内。如何让 Tiles 在评估 template.jsp 后评估 content.jsp

I have a problem where the nested templates are being evaluated before the parent template. Due to the ordering issue, this is causing issues with a tag library that insists nesting.

template.jsp

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://taglibs.com/tags" prefix="s"%>
<%System.out.println("evaluating template.jsp");%>
<s:outer>
    <tiles:insertAttribute name="content" />
</s:outer>

content.jsp

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://taglibs.com/tags" prefix="s"%>
<tiles:insertDefinition name="template">
    <tiles:putAttribute name="content" />
        <%System.out.println("evaluating content.jsp");%>
        <s:inner />
    </tiles:putAttribute>
</tiles:insertDefinition>

When the content.jsp page is evaluated, the following is output.

evaluating content.jsp
evaluating template.jsp

The content.jsp page is failing because the inner tag must be inside the outer tag. How do I get Tiles to evaluate content.jsp after it has evaluated template.jsp?

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

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

发布评论

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

评论(1

天煞孤星 2024-10-10 14:50:41

我可能是错的,但我认为 Tiles 需要解析整个 JSP,这意味着您不能仅按照它的布局方式进行操作。当前布局来自目标 JSP,然后在内部使用模板。我们可以使用tiles定义来指定模板作为填充内容的目标。我认为这种方法可以让您更有效地使用图块继承,并且可以解决这个问题,因为“模板”将首先渲染,而不是第二个。

例子:
tiles.xml

<tiles-definitions>
    <definition name="myTemplate" template="/WEB-INF/jsf/template.jsp">
         <put-attribute name="content" value="" />
    </definition>
    <definition name="content" extends="myTemplate">
         <put-attribute name="content" value="content.jsp" />
    </definition>
</tiles-definitions>

现在您必须指定一个tile 定义作为您的结果类型...如果您使用的是struts2,我会告诉您,但很可能所有框架都不是。

您的模板 jsp 看起来将保持不变。
并且您的内容 jsp 将被重写,如下所示:

<%@taglib uri="http://taglibs.com/tags" prefix="s"%>
<%System.out.println("evaluating content.jsp");%>
<s:inner />

请注意,现在无需将任何图块逻辑混合到页面中。内容仅此而已。您会注意到,tiles.xml 文件比我真正需要的更复杂,但我想表明,在我们的示例中,我们只有一个“put 属性”,但在更复杂的页面中,您可以有很多,因此“myTemplate”会生成一个页面不仅包含内容,还包含页眉、页脚和侧边栏,甚至还包含页眉,这样您就可以更改页面上的脚本和 CSS,然后您只需按照给定的示例扩展它,然后仅更改您想要的部分(通常它只是内容),您可以进一步扩展该定义...基本布局 ->安全布局扩展了 Base,但可能会更改标题以包含锁定图标 -> View_users_in_secure_layout 扩展了安全布局,更改了查看用户功能的内容。

I could be wrong but I think Tiles needs to parse the whole JSP, which means you can't do it just the way it is laid out. The current layout goes from a target JSP which then uses a template internally. We can use tiles definitions to specify the template as the target filling in the content. I think this method will let you use tiles inheritance more efficiently and may solve this issue because the "template" will be rendered first and not second.

Example:
tiles.xml

<tiles-definitions>
    <definition name="myTemplate" template="/WEB-INF/jsf/template.jsp">
         <put-attribute name="content" value="" />
    </definition>
    <definition name="content" extends="myTemplate">
         <put-attribute name="content" value="content.jsp" />
    </definition>
</tiles-definitions>

Now you must specify a tiles defintion as your result type... If you're using struts2 I'd tell you but chances are with all the frameworks your not.

Your template jsp would stay looking the same.
and your content jsp would be rewritten something like:

<%@taglib uri="http://taglibs.com/tags" prefix="s"%>
<%System.out.println("evaluating content.jsp");%>
<s:inner />

Note there is now no need to mix any tiles logic into the page. The content is just that. You'll notice that the tiles.xml file is more complicated than I really needed but I wanted to show that in our example we only have one 'put attribute' but in more complicated pages you can have many and so 'myTemplate' would produce a page with includes not only for content but header and footer and side bars perhaps even headers so you can change the scripts and CSS on the page and then you simply extend it as in the example given and then change only the pieces you want (typically it is only the content), further you can extend that definition... Base Layout -> Secure Layout extends Base but perhaps changes the header to include a lock icon -> View_users_in_secure_layout extends secure layout changing the content for the view users function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文