how do I generate reusable template content with embedded code in a JSP 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 without 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

  • Dynamic includes (server side includes)
  • Tag files
  • Static includes will not work in this scenario as there will be variable name conflicts.

    Yes, tag files have their own scope.

    链接地址: http://www.djcxy.com/p/90130.html

    上一篇: Struts或Tiles OR ...... JSP模板解决方案

    下一篇: 如何在JSP页面中使用嵌入式代码生成可重用模板内容?