將HTML 檔案轉換為JSP 專案時,JSP 範本非常有用,但可能缺乏範本繼承和基礎檔案等高階功能支援。雖然動態路由看起來像是解決方案,但 JSP 2.0 標記檔案提供了一種更簡單、更可自訂的方法。
<%@tag description="Simple Wrapper Tag" pageEncoding="UTF-8"%> <html><body> <jsp:doBody/> </body></html>
用法:
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="t" tagdir="/WEB-INF/tags" %> <t:wrapper> <h1>Welcome</h1> </t:wrapper>
<%@tag description="Overall Page template" pageEncoding="UTF-8"%> <%@attribute name="header" fragment="true" %> <%@attribute name="footer" fragment="true" %> <html> <body> <div>
用法:
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="t" tagdir="/WEB-INF/tags" %> <t:genericpage> <jsp:attribute name="header"> <jsp:include page="header.jsp" /> </jsp:attribute> <jsp:attribute name="footer"> <jsp:include page="footer.jsp" /> </jsp:attribute> <jsp:body> <h2>My Awesome Content</h2> </jsp:body> </t:genericpage>
<%@tag description="User Page template" pageEncoding="UTF-8"%> <%@taglib prefix="t" tagdir="/WEB-INF/tags" %> <%@attribute name="userName" required="true"%> <t:genericpage> <jsp:attribute name="header"> <h1>Welcome ${userName}</h1> </jsp:attribute> <jsp:attribute name="footer"> <p>
用性(userpage.tag)
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="t" tagdir="/WEB-INF/tags" %> <t:userpage userName="${user.fullName}"> <p> First Name: ${user.firstName} <br/> Last Name: ${user.lastName} <br/> Phone: ${user.phone}<br/> </p> </t:userpage>
<%@tag description="User Detail fragment" 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/>
使用可重複使用邏輯的可擴充元件(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>
以上是JSP 標記檔案如何增強 JSP 專案中的範本化和可重複使用性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!