tangguo

JSP技巧使模板制作更容易?

java

在工作中,我的任务是将一堆HTML文件变成一个简单的JSP项目。实际上,这完全是静态的,不需要编写服务器端逻辑。我应该提到我是Java的新手。JSP文件似乎使使用通用包含和变量变得很容易,就像PHP,但是我想知道一种简单的方法来获得诸如模板继承(Django样式)之类的东西,或者至少能够使base.jsp文件包含页眉和页脚,以便稍后插入内容。

Ben Lings似乎在这里提供了一些希望: JSP模板继承 有人可以解释如何实现这一点吗?

鉴于我没有太多时间,所以我认为动态路由会花很多时间,因此很高兴能将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但是随后我需要为每个包装程序添加一个顶部和一个底部,这有点混乱。


阅读 263

收藏
2020-10-19

共1个答案

小编典典

正如skaffman所建议的那样,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>

但这使您喜欢在其他地方使用该用户详细信息块。因此,我们将对其进行重构。 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 Tag FilesTiles至少对我来说,有很多篡改之类的东西。我发现它们更易于使用,因为唯一的结构就是您提供的结构,没有任何先入为主的构想。另外,您可以将JSP标记文件用于其他用途(例如上面的用户详细信息片段)。

这是一个与我已经完成的DisplayTag相似的示例,但是所有这些都是通过Tag Files(和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 tags(如c:if,等等)。在标记文件标记的主体内,您唯一不能做的就是添加Java scriptlet代码,但这并没有您想象的那么多限制。如果我需要scriptlet的东西,我只是将逻辑放入标签中,然后将标签放入其中。

因此,标记文件几乎可以是任何您想要的文件。在最基本的级别上,它是简单的剪切和粘贴重构。抓取一部分布局,将其切出,进行一些简单的参数化,然后将其替换为标记调用。

在更高的级别上,您可以做一些复杂的事情,例如我在这里使用的这个表格标签。

2020-10-19