【发布时间】:2017-02-04 02:32:45
【问题描述】:
似乎有两种使用 JSP 进行模板化的方法。包含具有这些语句之一的文件
<%@ include file="foo.html" %>
<jsp:include page="foo.html" />
或者使用JSP标签文件
// Save this as mytag.tag
<%@ tag description="Description" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<jsp:doBody/>
</body>
</html>
并在另一个 JSP 页面中调用它
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:mytag>
<h1>Hello World</h1>
</t:mytag>
那么我应该使用哪种方法呢?一种现在被认为已弃用,还是它们既有效又涵盖不同的用例?
编辑
使用这个标签文件和使用包含不一样吗?
// Save this as product.tag
<%@ tag description="Product templage" pageEncoding="UTF-8"%>
<%@ tag import="com.myapp.Product" %>
<%@ attribute name="product" required="true" type="com.myapp.Product"%>
Product name: ${product.name} <br/>
Quantity: ${product.quantity} <br/>
然后在另一个 JSP 上调用它
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:product>
<c:forEach items="${cart.products}" var="product">
<t:product product="${product}"/>
</c:forEach>
</t:product>
在我看来,这与使用包含并向其传递参数非常相似。那么标记文件与包含相同吗?
【问题讨论】:
标签: java jsp include jsp-tags tagfile