自定义JSTL标签与正文(Custom JSTL tags with body)

我们将在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标记内使用body-content ...

这有效:

<mytags:template content="some content... not HTML" />

但在我们的案例中并不是很有用。

We are going to use JSTL and custom JSTL-tags for some sort of template-engine in our JSP/spring-project.

Is there a way to create a tag that looks similar like this:

<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>

and use it like this:

<mytags:template> <h1>Title</h1> <p>My content!</p> </mytags:template>

i.e. use body-content inside a custom JSTL-tag...

This works:

<mytags:template content="some content... not HTML" />

but is not very useful in our case.

最满意答案

类似于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>

主要优点是能够有两个片段,而不是只有一个片段。

Similar to McDowell's answer, but with more flexibility, is to declare an attribute that is a fragment. http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags5.html#wp89854

e.g., //foo.tag tag file

<%@ 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 file

<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>

this will produce this markup:

<h1><b>a fancy</b> hello</h1> <p>body: <em><pre>more fancy body</pre></em></p> </body>

The main advantage is to be able to have two fragments, instead of just one with a tag.

自定义JSTL标签与正文(Custom JSTL tags with body)

我们将在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标记内使用body-content ...

这有效:

<mytags:template content="some content... not HTML" />

但在我们的案例中并不是很有用。

We are going to use JSTL and custom JSTL-tags for some sort of template-engine in our JSP/spring-project.

Is there a way to create a tag that looks similar like this:

<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>

and use it like this:

<mytags:template> <h1>Title</h1> <p>My content!</p> </mytags:template>

i.e. use body-content inside a custom JSTL-tag...

This works:

<mytags:template content="some content... not HTML" />

but is not very useful in our case.

最满意答案

类似于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>

主要优点是能够有两个片段,而不是只有一个片段。

Similar to McDowell's answer, but with more flexibility, is to declare an attribute that is a fragment. http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags5.html#wp89854

e.g., //foo.tag tag file

<%@ 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 file

<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>

this will produce this markup:

<h1><b>a fancy</b> hello</h1> <p>body: <em><pre>more fancy body</pre></em></p> </body>

The main advantage is to be able to have two fragments, instead of just one with a tag.