Home > Java > javaTutorial > How can JSP 2.0 Tag Files be used for efficient HTML template inheritance?

How can JSP 2.0 Tag Files be used for efficient HTML template inheritance?

Mary-Kate Olsen
Release: 2024-12-02 02:45:09
Original
483 people have browsed it

How can JSP 2.0 Tag Files be used for efficient HTML template inheritance?

JSP Template Inheritance for HTML Templating

JSP 2.0 Tag Files offer a simple and versatile approach to template inheritance. Here's how to achieve it:

Base Template (base.tag)

<%@tag description="Simple Wrapper Tag" pageEncoding="UTF-8"%>
<html><body>
  <jsp:doBody/>
</body></html>
Copy after login

Example Page (example.jsp)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:wrapper>
    <h1>Welcome</h1>
</t:wrapper>
Copy after login

This will output:

<html><body>
<h1>Welcome</h1>
</body></html>
Copy after login

Extending the Template

To add header and footer sections to a page:

Generic Page Template (genericpage.tag)

<%@tag description="Overall Page template" pageEncoding="UTF-8"%>
<%@attribute name="header" fragment="true" %>
<%@attribute name="footer" fragment="true" %>
<html>
  <body>
    <div>
Copy after login

Example Page (userpage.jsp)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:genericpage>
    <jsp:attribute name="header">
      <h1>Welcome ${userName}</h1>
    </jsp:attribute>
    <jsp:attribute name="footer">
      <p>
Copy after login

Reusable User Detail Fragment (userdetail.tag)

<%@tag description="User Page template" pageEncoding="UTF-8"%>
<%@tag import="com.example.User" %>
<%@attribute name="user" required="true" type="com.example.User"%>

First Name: ${user.firstName} <br/>
Last Name: ${user.lastName} <br/>
Phone: ${user.phone}<br/>
Copy after login

Example Page (using userdetail.tag)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:userpage userName="${user.fullName}">
  <p>
    <t:userdetail user="${user}"/>
  </p>
</t:userpage>
Copy after login

The above is the detailed content of How can JSP 2.0 Tag Files be used for efficient HTML template inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template