Java JSP 模板继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/490390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP template inheritance
提问by Ryan
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 :)
来自 Django 的背景,我经常使用“模板继承”,其中多个模板从一个公共基础继承。有没有一种简单的方法可以在 JSP 中做到这一点?如果没有,是否有替代 JSP 的方法(除了 Jython 上的 Django 是 :)
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")
将呈现如下(假设 conten.title 是“在此处插入标题”,而 content.body 是“在此处插入正文”)
<html>
<body>
<h1>Insert title Here <-- Fills in a variable</h1>
Insert Body Here <-- Fills in another variable
</body>
<html>
采纳答案by geowa4
回答by yawmark
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.
其他值得探索的选项包括Sitemesh,它建立在页面装饰器的思想之上,以及Java Server Faces(JSF),它使用基于 Web 的 UI 组件。当我们谈论在 Java 平台上使用 Web 框架进行快速开发时,我鼓励您查看Grails。它与 Django 有着相同的使命;即,基于约定优于配置的快速 Web 应用程序开发。
Hope that's not too many suggestion for one post. :o)
希望对一篇文章的建议不要太多。:o)
回答by Ben Lings
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.
您可以使用 JSP 标记文件做类似的事情。创建您自己的page.tag
包含页面结构的内容。然后使用<jsp:body/>
标签插入内容。
回答by jaketrent
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.
我最喜欢的 Java Web 前端技术是 Facelets。它支持我见过的最像 Django 的模板。它不像 Django 的那么干净,但您可以获得相同的继承优势。
Instead of Django's:
而不是Django的:
Super:
极好的:
{% block content %}{% endblock %}
Sub:
子:
{% block content %}inheriting template's content here{% endblock %}
Facelet's syntax is like this:
Facelet 的语法是这样的:
Super:
极好的:
<ui:insert name="content"></ui:insert>
Sub:
子:
<ui:define name="content">inheriting template's content here</ui:define>
回答by badqiu
You can use rapid-framework for JSP template inheritance
您可以使用快速框架进行 JSP 模板继承
base.jsp
基础.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
子.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
源代码
回答by Gelin Luo
Rythm Template enginehas implemented an elegant approach for template inheritance.
节奏模板引擎实现了一种优雅的模板继承方法。
So suppose your layout template (parent template) called main.html
:
因此,假设您的布局模板(父模板)称为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
在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.
可以在http://www.playframework.org/modules/rythm找到一份综合文档。虽然它是针对 Play!Framework 的,但大部分内容也适用于没有 Play!Framework 的纯节奏引擎。