JSP template inheritance

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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

        {% block content %}     {% endblock %}    

basic content

{% extends "base template" %} {% block content %} 

{{ content.title }} <-- Fills in a variable

{{ 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")

        

Insert title Here <-- Fills in a variable

Insert Body Here <-- Fills in another variable

回答1:

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.



回答2:

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



回答3:

You can use rapid-framework for JSP template inheritance

base.jsp

%@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid" %>                                base_head_content                                
base_body_content

child.jsp

<%@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid" %>           

Entry one

This is my first entry.

<%@ include file="base.jsp" %>

output

     base_head_content           

Entry one

This is my first entry.

source code

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



回答4:

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)



回答5:

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:

Sub:

inheriting template's content here


回答6:

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

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

@get("title", "default main page")

@render("leftPanel")
@render("rightPanel")
@render()

And here is your target template:

@extends(main) @set(title: "My Cool Page") @section("leftPanel") {  }  @section("rightPanel") { 
...
} @*** 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.



转载请标明出处:JSP template inheritance
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!