将呈现的JSP片段作为参数传递给另一个JSP(Passing a rendered JSP fragment as an argument to another JSP)

编程入门 行业动态 更新时间:2024-10-23 21:28:16
将呈现的JSP片段作为参数传递给另一个JSP(Passing a rendered JSP fragment as an argument to another JSP)

我有一个JSP片段,如下所示:

<div> <div> <!-- 1 --> <h1>Hello Everyone!</h1> </div> <div> <!-- 2 --> <jsp:include page="./anotherJspFragment.jsp"> <jsp:param name="someName" value="someValue"/> </jsp:include> </div> </div>

这个片段在JSP页面中重复多次 - 它已被复制和粘贴,只有标记为“1”和“2”的部分在每次出现之间发生变化。

此片段的每个实例都include一个不同的JSP页面,具有不同的参数。

有些看起来像这样:

<jsp:include page="./firstJspFragment.jsp" />

而其他人看起来像这样:

<jsp:include page="./secondJspFragment.jsp"> <jsp:param name="someName1" value="someValue1"/> <jsp:param name="someName2" value="someValue2"/> </jsp:include>

我想创建一个可重用的JSP片段,其中包含所有样板标记,我可以使用它来减少重复。 此JSP片段需要将呈现的JSP片段作为参数( firstJspFragment.jsp或secondJspFragment.jsp等)。 这可能吗? 或者我可以使用其他方法吗?

I have a JSP fragment that looks like this:

<div> <div> <!-- 1 --> <h1>Hello Everyone!</h1> </div> <div> <!-- 2 --> <jsp:include page="./anotherJspFragment.jsp"> <jsp:param name="someName" value="someValue"/> </jsp:include> </div> </div>

This fragment is repeated several times within a JSP page - it has been copied and pasted, with only the parts labelled '1' and '2' changing between each occurrence.

Each instance of this fragment includes a different JSP page, with different arguments.

Some look like this:

<jsp:include page="./firstJspFragment.jsp" />

While others look like this:

<jsp:include page="./secondJspFragment.jsp"> <jsp:param name="someName1" value="someValue1"/> <jsp:param name="someName2" value="someValue2"/> </jsp:include>

I would like to create a reusable JSP fragment that contains all the boilerplate markup, which I can use to reduce the duplication. This JSP fragment would need to take a rendered JSP fragment as an argument (firstJspFragment.jsp or secondJspFragment.jsp, etc). Is this possible? Or is there some other approach I can use?

最满意答案

好的,所以看起来这样做的方法是在WEB-INF/tags创建一个名为myTag.tag WEB-INF/tags ,其中包含样板标记

http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html

然后,您可以将渲染的页面作为正文传递,例如

<test:myTag title="Hello Everyone"> <jsp:include page="./firstJspFragment.jsp" /> </test:myTag>

然后,将在标记中的<jsp:doBody />出现的位置渲染此JSP片段,例如

<%@ attribute name="title" required="true" %> <div> <div> <!-- 1 --> <h1>${title}</h1> </div> <div> <!-- 2 --> <jsp:doBody /> </div> </div>

Ok, so it looks like the way to do this is to create a tag in WEB-INF/tags called e.g. myTag.tag which contains the boilerplate markup

http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html

You can then pass the rendered page in as the body, e.g.

<test:myTag title="Hello Everyone"> <jsp:include page="./firstJspFragment.jsp" /> </test:myTag>

This JSP fragment will then be rendered at the point in the tag where <jsp:doBody /> occurrs, e.g.

<%@ attribute name="title" required="true" %> <div> <div> <!-- 1 --> <h1>${title}</h1> </div> <div> <!-- 2 --> <jsp:doBody /> </div> </div>

更多推荐

JSP,jsp,fragment,片段,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-07-30 23:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   参数   JSP   Passing   argument

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!