views:

1407

answers:

7

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, is there an alternative to JSP that does this (besides Django on Jython that is :)

base template

<html>
  <body>
    {% block content %}
    {% endblock %}
  </body>
<html>

basic content

{% extends "base template" %}
{% block content %}
<h1>{{ content.title }} <-- Fills in a variable</h1>
{{ content.body }} <-- Fills in another variable
{% endblock %}

Will render as follows (assuming that conten.title is "Insert Title Here", and content.body is "Insert Body Here")

<html>
  <body>
    <h1>Insert title Here <-- Fills in a variable</h1>
    Insert Body Here <-- Fills in another variable
  </body>
<html>
+4  A: 

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.

geowa4
Oops, you beat me to it, deleted my answer. :) +1 for Tiles...
Jeff
I've never tried tiles and just skimmed the documentation, but I'm curious whether my perception is correct: it seems there's a lot of setup (config files + need to write java classes)? The great thing about Django templates is that they pretty much just work, inheritance and such are super-easy to do with just the parent template file and the child template file, no need for additional configuration.
Roy Tang
+2  A: 

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)

yawmark
A: 

I'd be curious to know what standard practice in Grails is for "template inheritance" I've just started with grails and see that there are a few different ways provided for inheriting pieces of HTML both from the gsp and from the controller logic....

ראובן
+1  A: 

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.

Ben Lings
+1  A: 

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>
jtsnake
A: 

Right now i am going through similar pain of working on a web-project that has jsp based view on spring mvc. What started as a single project has spawned a lot of child projects where basically certain controllers and views are to be overridden . The heart burn comes when in certain flows only certain portion of the views are to be overrideen and we end up having to duplicate the entire jsp and the other alternative to this being breaking up jsps into a lot of constituent portions has a basic issue of determining the granularity of breakups and sometimes results in a lot of small portions to be included in a umbrella jsp that constitutes the view. One more option that comes to mind are java based views that internally wrap velocity temlates which provide a very good way of extension through inheritance. Is there a better approach than these both involving jsps ?? share ur views please.

redzedi
A: 

you can use rapid-framework for JSP template inheritance

base.jsp

%@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid" %>  
<html>  
<haed>
    <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>
<haed>  
 base_head_content
</haed>  
<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/

badqiu