Tiles 2 访问模板中的变量
我正在使用带有 Apache Tiles 2 的 Spring MVC 框架。我希望能够让多个控制器都使用相同的视图(不同的逻辑,一些基本的表示)。我可以轻松做到这一点。我现在想要的是为每个控制器拥有不同的 Tiles 定义,所有控制器都使用相同的 JSP 文件,但每个控制器传递不同的模板变量(页面标题、简短描述等)。这是我的 Tiles 模板定义文件:
<tiles-definitions>
<!-- Default Main Template -->
<definition name=".mainTemplate" template="/WEB-INF/templates/main.jsp">
<put-attribute name="shortTitle" value="Company ABC" type="string" />
<put-attribute name="body" value="/WEB-INF/templates/blank.jsp" />
</definition>
<!-- Overriding Templates -->
<definition name="index" extends=".mainTemplate">
<put-attribute name="title" value="Company Alpha Bravo Charlie" type="string" />
<put-attribute name="body" value="/WEB-INF/views/index.jsp" />
</definition>
<definition name="index2" extends=".mainTemplate">
<put-attribute name="title" value="Company Other Page" type="string" />
<put-attribute name="body" value="/WEB-INF/views/index.jsp" />
</definition>
</tiles-definitions>
然后我尝试使用这个 /WEB-INF/views/index.jsp
:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<h1>
Hello world, it's <tiles:getAsString name="title" />!
</h1>
当我将其加载到 Tomcat 并打开页面时,我得到了很长的异常堆栈跟踪。堆的顶部显示 org.apache.tiles.impl.CannotRenderException: ServletException 包括路径 '/WEB-INF/templates/main.jsp'.} 其根本原因 org.apache.tiles.template.NoSuchAttributeException: Attribute找不到“标题”。有人知道发生了什么事吗?
I am using the Spring MVC framework with Apache Tiles 2. I want to be able to have multiple controllers all use the same view (different logic, some basic presentation). I can do that easily. What I want now is to have different Tiles definitions for each controller, all using the same JSP file, but each passing different template variables (page header, short description, etc). This is my Tiles template definition file:
<tiles-definitions>
<!-- Default Main Template -->
<definition name=".mainTemplate" template="/WEB-INF/templates/main.jsp">
<put-attribute name="shortTitle" value="Company ABC" type="string" />
<put-attribute name="body" value="/WEB-INF/templates/blank.jsp" />
</definition>
<!-- Overriding Templates -->
<definition name="index" extends=".mainTemplate">
<put-attribute name="title" value="Company Alpha Bravo Charlie" type="string" />
<put-attribute name="body" value="/WEB-INF/views/index.jsp" />
</definition>
<definition name="index2" extends=".mainTemplate">
<put-attribute name="title" value="Company Other Page" type="string" />
<put-attribute name="body" value="/WEB-INF/views/index.jsp" />
</definition>
</tiles-definitions>
I then try to have this /WEB-INF/views/index.jsp
:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<h1>
Hello world, it's <tiles:getAsString name="title" />!
</h1>
When I load this into Tomcat and bring up the page, I get a long stack trace of exceptions. The top of the pile says org.apache.tiles.impl.CannotRenderException: ServletException including path '/WEB-INF/templates/main.jsp'.} with root cause org.apache.tiles.template.NoSuchAttributeException: Attribute 'title' not found
. Anybody know what's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你如何尝试这个?在控制器中,您将指定 Tiles 视图的名称,而不是使用多个 JSP Tiles 之一来呈现页面:
How do you try this? In your controller you would specify the name of the Tiles view, not one of the multiple JSP Tiles will use in order to render the page: