开发者

how do I generate reusable template content with embedded code in a JSP page?

开发者 https://www.devze.com 2023-03-18 08:52 出处:网络
I\'m trying to find a simple solutions to generate markup in a JSP page that needs to be reused within that page.

I'm trying to find a simple solutions to generate markup in a JSP page that needs to be reused within that page.

The templated markup would have scriptlet calls within it, with some localized Java declarations.

For example:

<markup>
<% MyObject localObject = controller.getMyObject(); %>
<name><%= localObject.getName() %></name>
<value><%= localObject.getValue() %></value>
</markup>

I want to be able to reuse the construct above multiple times in a JSP file wit开发者_JAVA技巧hout there being any multiple local variable conflicts.

Is there a best practice for doing this in JSP? Do tag files introduce their own level of scope in terms of local declarations as opposed to a simple include directive?

EDIT: I also need the local variable to be passed to the templated structure.


There are 2 options

  1. Dynamic includes (server side includes)
  2. Tag files

Static includes will not work in this scenario as there will be variable name conflicts.

Yes, tag files have their own scope.

0

精彩评论

暂无评论...
验证码 换一张
取 消