JSP技巧使模板更容易? [英] JSP tricks to make templating easier?

查看:119
本文介绍了JSP技巧使模板更容易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我的任务是将一堆 HTML 文件转换为一个简单的 JSP 项目。它实际上是静态的,无需编程的服务器端逻辑。我应该提到我对Java完全不熟悉。 JSP文件似乎可以很容易地使用常见的包含和变量,就像 PHP ,但我想知道一种简单的方法来获得类似模板继承的东西( Django style)或者至少能够有一个包含页眉和页脚的base.jsp文件,所以我可以稍后插入内容。

At work I've been tasked with turning a bunch of HTML files into a simple JSP project. It's really all static, no serverside logic to program. I should mention I'm completely new to Java. JSP files seem to make it easy to work with common includes and variables, much like PHP, but I'd like to know a simple way to get something like template inheritance (Django style) or at least be able to have a base.jsp file containing the header and the footer, so I can insert content later.

Ben Lings 似乎在他的答案中提供了一些希望:
JSP模板继承
有人可以解释如何实现这个目标吗?

Ben Lings seems to offer some hope in his answer here: JSP template inheritance Can someone explain how to achieve this?

鉴于我没有太多时间我认为动态路由有点多,所以我很高兴让URL直接映射到 .jsp 文件,但我愿意接受建议。

Given that I don't have much time I think dynamic routing is a little much, so I'm happy to just to have URLs map directly onto .jsp files, but I'm open to suggestion.

谢谢。

编辑:我不想使用任何外部图书馆,因为它会增加我自己和项目工作的其他人以及公司I的学习曲线已经签约工作。

edit: I don't want to use any external libraries, because it would increase the learning curve for myself and others who work on the project, and the company I work for has been contracted to do this.

另一个编辑:我不确定 JSP标签将非常有用,因为我的内容实际上没有任何模板变量。我需要的是一种能够做到这一点的方法:

Another edit: I'm not sure if JSP tags will be useful because my content doesn't really have any template variables. What I need is a way to be able to do this:

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 来实现,但是我需要为每个包装器提供一个顶部和底部包含,这有点混乱。

I think this would give me enough versatility to do everything I need. It could be achieved with includes but then I would need a top and a bottom include for each wrapper, which is kind of messy.

推荐答案

skaffman建议 JSP 2.0标记文件是蜜蜂的膝盖。

让我们举一个简单的例子。

Let's take your simple example.

放下以下内容在 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

So, lets expand upon that to something a bit more general. 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>

这会给你带来什么?真的很多,但它变得更好......

What does that buy you? A lot really, but it gets even better...

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>

使用它:
(假设我们在请求中有一个用户变量)

To use this: (assume we have a user variable in the request)

<%@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标记文件的优点在于它让它你基本上标记了通用标记,然后将它重构为你心中的内容。


The beauty of JSP Tag files is that it lets you basically tag generic markup and then refactor it to your heart's content.

JSP标记文件有很多篡改的东西喜欢 Tiles 等,至少对我而言。我发现它们更容易使用,因为唯一的结构是你给它的,没有任何先入为主。另外,您可以将JSP标记文件用于其他内容(例如上面的用户详细信息片段)。

JSP Tag Files have pretty much usurped things like Tiles etc., at least for me. I find them much easier to use as the only structure is what you give it, nothing preconceived. Plus you can use JSP tag files for other things (like the user detail fragment above).

这是一个与我已经完成的DisplayTag类似的示例,但是这个完成所有标记文件(和 Stripes 框架,这是s:标签..)。这会产生一个行表,交替颜色,页面导航等表:

Here's an example that is similar to DisplayTag that I've done, but this is all done with Tag Files (and the Stripes framework, that's the s: tags..). This results in a table of rows, alternating colors, page navigation, etc:

<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的东西,我只需将逻辑放入标签并放入标签。轻松。

Of course the tags work with the JSTL tags (like c:if, etc.). The only thing you can't do within the body of a tag file tag is add Java scriptlet code, but this isn't as much of a limitation as you might think. If I need scriptlet stuff, I just put the logic in to a tag and drop the tag in. Easy.

因此,标签文件几乎可以是你希望他们成为。在最基本的层面上,它是简单的剪切和粘贴重构。抓取一块布局,剪切掉,做一些简单的参数化,然后用标签调用替换它。

在更高级别,你可以做像这个表标签这样复杂的东西我在这里。

At a higher level, you can do sophisticated things like this table tag I have here.

这篇关于JSP技巧使模板更容易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆