JSP, can it work similar to yield, layout, content_for in Ruby/Rails/Erb

前端 未结 2 1557
我在风中等你
我在风中等你 2020-12-16 02:46

I am trying to figure out how to most effectively reuse JSP code. I love the way Rails/erb works in that way ... with yield, layout, content_for

Example:

mai

相关标签:
2条回答
  • 2020-12-16 03:32

    I'm not familiar with what yield and content_for provide, but JSP tag files allow you a more robust way to template pages than JSP includes.

    Example:

    layout.tag

    <%@ tag body-content="scriptless" %>
    <%@ attribute name="pageTitle" required="true" type="java.lang.String" %>
    
    <html>
    <head>
        <title>${pageTitle}</title>
    </head>
    <body>
        <jsp:doBody/>
    </body>
    </html>
    

    An individual JSP

    <%@ taglib prefix="z" tagdir="/WEB-INF/tags" %>
    <z:layout pageTitle="A simple page">
        <p>Hello, JSP!</p>
    </z:layout>
    

    Just place your layout.tag in the /WEB-INF/tags directory. You can use any available prefix you want, I just used "z" for the example.

    0 讨论(0)
  • 2020-12-16 03:46

    While you mentioned wanting no frameworks on top of stock jsp, the Layout functionality of the Stripes Framework does pretty much exactly what you're asking for.

    0 讨论(0)
提交回复
热议问题