我们将在我们的 JSP/spring-project 中使用 JSTL 和自定义 JSTL 标签作为某种模板引擎。
有没有办法创建一个看起来像这样的标签:
<div id="site">
<div id="header">Some title</div>
<div id="navigation"> SOME DYNAMIC CONTENT HERE </div>
<div id="content"> ${content} </div>
<div id="footer"></div>
</div>
并像这样使用它:
<mytags:template>
<h1>Title</h1>
<p>My content!</p>
</mytags:template>
即在自定义 JSTL 标签中使用正文内容...
这有效:
<mytags:template content="some content... not HTML" />
但在我们的情况下不是很有用。
最佳答案
类似于 McDowell 的答案,但更灵活的是声明一个片段属性。 http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags5.html#wp89854
例如。,
//foo.tag标签文件
<%@ attribute name="greeting" fragment="true" %>
<%@ attribute name="body" fragment="true" %>
<h1><jsp:invoke fragment="greeting" /></h1>
<p>body: <em><jsp:invoke fragment="body" /></em></p>
jsp文件
<x:foo>
<jsp:attribute name="greeting"><b>a fancy</b> hello</jsp:attribute>
<jsp:attribute name="body"><pre>more fancy body</pre></jsp:attribute>
</x:foo>
这将产生这个标记:
<h1><b>a fancy</b> hello</h1>
<p>body: <em><pre>more fancy body</pre></em></p>
</body>
主要优点是能够有两个片段,而不仅仅是一个带有标签的片段。
关于jsp - 带有正文的自定义 JSTL 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1593532/