Java – JSP, which can be like yield, layout and content in ruby / rails / Erb_ For similar

I'm trying to figure out how to reuse JSP code most effectively

Example:

main_ layout. erb. html

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

use

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

<p>Hello,Rails!</p>

In controller

layout "main_layout"

What is the closest thing to JSP (without using additional frameworks)? I know JSP contains, but it's not exactly the same Any suggestions?

thank you

Solution

I'm not familiar with yield and content_ For, but JSP tag files allows you to include more powerful methods of template pages than JSP

Example:

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>

Personal JSP

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

Just put your layout Tag is placed in the / Web - inf / tags directory You can use any available prefix you want. I just used "Z" as an example

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>