在工作中,我的任务是将一堆 HTML
文件转换为一个简单的 JSP
项目。它实际上都是静态的,没有要编程的服务器端逻辑。我应该提到我对 Java 完全陌生。 JSP 文件似乎可以很容易地使用常见的包含和变量,很像 PHP
,但我想知道一种简单的方法来获得模板继承之类的东西(Django
style) 或者至少能够有一个包含页眉和页脚的 base.jsp 文件,这样我以后可以插入内容。
Ben Lings 似乎在他的回答中提供了一些希望: JSP template inheritance 有人可以解释如何实现这一目标吗?
鉴于我没有太多时间,我认为动态路由有点多,所以我很高兴将 URL 直接映射到 .jsp
文件,但我愿意建议。
谢谢。
编辑:我不想使用任何外部库,因为这会增加我自己和从事该项目的其他人的学习曲线,而且我工作的公司已与这样做。
另一个编辑:我不确定 JSP tags
是否有用,因为我的内容实际上没有任何模板变量。我需要的是一种能够做到这一点的方法:
base.html:
<html><body>
{ content.body }
</body></html>
somepage.html
<wrapper:base.html>
<h1>Welcome</h1>
</wrapper>
输出为:
<html><body>
<h1>Welcome</h1>
</body></html>
我认为这会给我足够的多功能性来做我需要的一切。它可以通过 includes
来实现,但是我需要为每个包装器添加一个顶部和底部包含,这有点困惑。
最佳答案
作为 skaffman suggested , JSP 2.0 标记文件是蜜蜂的膝盖。
让我们举个简单的例子。
将以下内容放入WEB-INF/tags/wrapper.tag
<%@tag description="Simple Wrapper Tag" pageEncoding="UTF-8"%>
<html><body>
<jsp:doBody/>
</body></html>
现在在您的 example.jsp
页面中:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:wrapper>
<h1>Welcome</h1>
</t:wrapper>
这正是你认为的那样。
所以,让我们将其扩展为更一般的东西。
WEB-INF/tags/genericpage.tag
<%@tag description="Overall Page template" pageEncoding="UTF-8"%>
<%@attribute name="header" fragment="true" %>
<%@attribute name="footer" fragment="true" %>
<html>
<body>
<div id="pageheader">
<jsp:invoke fragment="header"/>
</div>
<div id="body">
<jsp:doBody/>
</div>
<div id="pagefooter">
<jsp:invoke fragment="footer"/>
</div>
</body>
</html>
要使用这个:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:genericpage>
<jsp:attribute name="header">
<h1>Welcome</h1>
</jsp:attribute>
<jsp:attribute name="footer">
<p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p>
</jsp:attribute>
<jsp:body>
<p>Hi I'm the heart of the message</p>
</jsp:body>
</t:genericpage>
这能给你带来什么?真的很多,但它会变得更好......
WEB-INF/tags/userpage.tag
<%@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 id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p>
</jsp:attribute>
<jsp:body>
<jsp:doBody/>
</jsp:body>
</t:genericpage>
要使用它: (假设我们在请求中有一个用户变量)
<%@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>
但它让您喜欢在其他地方使用该用户详细信息 block 。所以,我们将对其进行重构。
WEB-INF/tags/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/>
现在前面的例子变成了:
<%@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 标记文件
几乎篡夺了诸如 Tiles
之类的东西,至少对我来说是这样。我发现它们更容易使用,因为唯一的结构是你给它的,没有先入为主的。另外,您可以将 JSP 标记文件用于其他事情(例如上面的用户详细信息片段)。
这是一个类似于我所做的 DisplayTag 的示例,但这都是使用标记文件(以及 Stripes
框架,即 s: 标签..)完成的。这会产生一个包含行、交替颜色、页面导航等的表格:
<t:table items="${actionBean.customerList}" var="obj" css_class="display">
<t:col css_class="checkboxcol">
<s:checkbox name="customerIds" value="${obj.customerId}"
onclick="handleCheckboxRangeSelection(this, event);"/>
</t:col>
<t:col name="customerId" title="ID"/>
<t:col name="firstName" title="First Name"/>
<t:col name="lastName" title="Last Name"/>
<t:col>
<s:link href="/Customer.action" event="preEdit">
Edit
<s:param name="customer.customerId" value="${obj.customerId}"/>
<s:param name="page" value="${actionBean.page}"/>
</s:link>
</t:col>
</t:table>
当然,这些标签与 JSTL 标签
一起工作(如 c:if
等)。在标记文件标记的正文中唯一不能做的事情是添加 Java scriptlet 代码,但这并没有您想象的那么大的限制。如果我需要 scriptlet 的东西,我只需将逻辑放入标签中并将标签放入。简单。
所以,标签文件几乎可以是你想要的任何东西。在最基本的层面上,它是简单的剪切和粘贴重构。抓取一大块布局,剪下来,做一些简单的参数化,然后用标签调用替换它。
在更高的层次上,你可以做一些复杂的事情,比如我在这里的这个表格标签。
https://stackoverflow.com/questions/1296235/