JSP,它的工作方式类似于Ruby / Rails / Erb中的yield,layout,content_for [英] JSP, can it work similar to yield, layout, content_for in Ruby/Rails/Erb

查看:202
本文介绍了JSP,它的工作方式类似于Ruby / Rails / Erb中的yield,layout,content_for的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何最有效地重用JSP代码。
我喜欢Rails / erb的工作方式......收益率,布局,content_for

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

示例:

main_layout.erb.html

main_layout.erb.html

<html>
  <head><%= yield :head %></head>
  <body><%= yield %></body>
</html>

使用

<% content_for :head do %>
<title>A simple page</title>
<% end %>

<p>Hello, Rails!</p>

在控制器中

layout "main_layout"

我可以通过JSP获得最接近的数据(没有使用额外的框架)?我知道JSP包含但是这与yield不同。
有什么建议吗?

What is the closest I can get to this with JSP (without using extra frameworks)? I know about JSP include but that's not really the same as yield. Any suggestions?

谢谢

推荐答案

我'我不熟悉 yield content_for 提供,但 JSP标记文件允许您比JSP包含更加健壮的模板页面方式。

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.

示例:

layout.tag

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>

单个JSP

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

只需将layout.tag放在/ WEB-INF / tags目录中。您可以使用您想要的任何可用前缀,我只使用z作为示例。

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.

这篇关于JSP,它的工作方式类似于Ruby / Rails / Erb中的yield,layout,content_for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆