JSP template inheritance

前端 未结 6 853
太阳男子
太阳男子 2020-12-13 14:13

Coming from a background in Django, I often use \"template inheritance\", where multiple templates inherit from a common base. Is there an easy way to do this in JSP? If not

相关标签:
6条回答
  • 2020-12-13 14:42

    Rythm Template engine has implemented an elegant approach for template inheritance.

    So suppose your layout template (parent template) called main.html:

    <h1>@get("title", "default main page")</h1>
    <div id="left-panel">@render("leftPanel")<div>
    <div id="right-panel">@render("rightPanel")</div>
    <div id="main-content">@render()</div>
    <div id="footer">
    @render("footer"){
       @**
        * the content here is supplied if the child template failed 
        * to provide it's own footer implementation
        *@
       <div class="footer">copyright 2012 ...</div>
    }
    </div>
    

    And here is your target template:

    @extends(main)
    @set(title: "My Cool Page")
    @section("leftPanel") {
    <ul class="menu">
    ...
    </ul>
    }
    
    @section("rightPanel") {
    <div class="news">
    ...
    </div>
    }
    
    @*** note no "footer" section supplied so the default content will be used **@
    
    @*** the rest is for the main content **@
    ...
    

    Check the real demo at http://rythmengine.com/demo/testdefaultlayoutcontent

    A comprehensive document could be found at http://www.playframework.org/modules/rythm. Though it's targeted to Play!Framework, most of the content also apply to pure rythm engine without Play!Framework.

    0 讨论(0)
  • 2020-12-13 14:50

    You'll probably want to look into Tiles.

    EDIT: On a related note to tiles, you might want to look into Struts. It's not what you're looking for (that's tiles), but it is useful for someone coming from Django.

    0 讨论(0)
  • 2020-12-13 14:51

    You can use rapid-framework for JSP template inheritance

    base.jsp

    %@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid" %>  
    <html>  
        <head>
            <rapid:block name="head">
                base_head_content
            </rapid:block>
        </head>  
        <body>  
            <br />  
            <rapid:block name="content">
                base_body_content
            </rapid:block>  
        </body>  
    </html>
    

    child.jsp

    <%@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid" %>  
    <rapid:override name="content">  
         <div>
            <h2>Entry one</h2>
            <p>This is my first entry.</p>
        </div>
    </rapid:override>  
    
    <!-- extends from base.jsp or <jsp:include page="base.jsp"> -->  
    <%@ include file="base.jsp" %> 
    

    output

    <html>
    <head>  
     base_head_content
    </head>  
    <body>  
        <br />  
        <div>
            <h2>Entry one</h2>
            <p>This is my first entry.</p>
        </div>
    </body>  
    </html>
    

    source code

    http://rapid-framework.googlecode.com/svn/trunk/rapid-framework/src/rapid_framework_common/cn/org/rapid_framework/web/tags/

    0 讨论(0)
  • 2020-12-13 14:52

    My favorite Java web front-end tech is Facelets. It supports the most Django-like templating I've seen. It's not quite as clean as Django's, but you get the same inheritance benefits.

    Instead of Django's:

    Super:

    {% block content %}{% endblock %}
    

    Sub:

    {% block content %}inheriting template's content here{% endblock %}
    

    Facelet's syntax is like this:

    Super:

    <ui:insert name="content"></ui:insert>
    

    Sub:

    <ui:define name="content">inheriting template's content here</ui:define>
    
    0 讨论(0)
  • 2020-12-13 14:53

    Other options worth exploring include Sitemesh, which is built on the idea of page decorators, and Java Server Faces (JSF), which employs web-based UI components. And while we're talking about rapid development with web frameworks on the Java platform, I encourage you to check out Grails. It has the same mission has Django; namely, rapid web app development based on convention over configuration.

    Hope that's not too many suggestion for one post. :o)

    0 讨论(0)
  • 2020-12-13 14:54

    You can do similar things using JSP tag files. Create your own page.tag that contains the page structure. Then use a <jsp:body/> tag to insert the contents.

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