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

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

问题描述

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

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

鉴于我没有太多时间,我认为动态路由有点多,所以我很高兴将 URL 直接映射到 .jsp 文件,但我是开放的给建议.

谢谢.

我不想使用任何外部库,因为这会增加我自己和从事该项目的其他人的学习曲线,而且我工作的公司已经签约这样做.

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

base.html:

{ content.body }</body></html>

somepage.html

<h1>欢迎</h1></包装器>

输出为:

<h1>欢迎</h1></body></html>

我认为这会给我足够的多功能性来做我需要的一切.它可以通过 includes 来实现,但是我需要为每个包装器设置一个顶部和一个底部包含,这有点混乱.

解决方案

正如 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:包装器><h1>欢迎</h1></t:wrapper>

这正是你认为的那样.

<小时>

所以,让我们将其扩展为更一般的东西.WEB-INF/tags/genericpage.tag

<%@tag description="整体页面模板" pageEncoding="UTF-8"%><%@attribute name="header" fragment="true" %><%@attribute name="footer" fragment="true" %><身体><div id="pageheader"><jsp:invoke fragment="header"/>

<div id="body"><jsp:doBody/>

<div id="pagefooter"><jsp:invoke fragment="footer"/>

使用:

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@taglib prefix="t" tagdir="/WEB-INF/tags" %><t:genericpage><jsp:attribute name="header"><h1>欢迎</h1></jsp:属性><jsp:attribute name="footer"><p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p></jsp:属性><jsp:body><p>我是消息的核心</p></jsp:body></t:genericpage>

这对你有什么好处?真的很多,但它变得更好......

<小时>

WEB-INF/tags/userpage.tag

<%@tag description="用户页面模板" pageEncoding="UTF-8"%><%@taglib prefix="t" tagdir="/WEB-INF/tags" %><%@attribute name="userName" required="true"%><t:genericpage><jsp:attribute name="header"><h1>欢迎 ${userName}</h1></jsp:属性><jsp:attribute name="footer"><p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p></jsp:属性><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>名字:${user.firstName} 
姓氏:${user.lastName}
电话:${user.phone}<br/></p></t:userpage>

<小时>

但是你会喜欢在其他地方使用该用户详细信息块.所以,我们将重构它.WEB-INF/tags/userdetail.tag

<%@tag description="用户页面模板" pageEncoding="UTF-8"%><%@tag import="com.example.User" %><%@attribute name="user" required="true" type="com.example.User"%>名字:${user.firstName} 
姓氏:${user.lastName}
电话:${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 Files 有很多篡改的东西,比如 Tiles 等,至少对我来说是这样.我发现它们更容易使用,因为唯一的结构就是你给它的,没有任何先入为主.此外,您还可以将 JSP 标记文件用于其他用途(例如上面的用户详细信息片段).

这是一个类似于我做过的 DisplayTag 的例子,但这都是用标签文件完成的(和 Stripes 框架,那就是 s: 标签..).这会产生一个包含行、交替颜色、页面导航等的表格:

<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="名字"/><t:col name="lastName" title="Last Name"/><t:col>编辑<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 的东西,我只需将逻辑放入一个标签中,然后将标签放入.简单.

所以,标签文件几乎可以是任何你想要的样子.在最基本的层面上,它是简单的剪切和粘贴重构.抓取一大块布局,剪下来,做一些简单的参数化,然后用标签调用替换它.

在更高的层次上,你可以做一些复杂的事情,比如我这里有的这个表格标签.

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 seems to offer some hope in his answer here: JSP template inheritance Can someone explain how to achieve this?

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.

Thanks.

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.

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>

with the output being:

<html><body>
<h1>Welcome</h1>
</body></html>

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.

解决方案

As skaffman suggested, JSP 2.0 Tag Files are the bee's knees.

Let's take your simple example.

Put the following in WEB-INF/tags/wrapper.tag

<%@tag description="Simple Wrapper Tag" pageEncoding="UTF-8"%>
<html><body>
  <jsp:doBody/>
</body></html>

Now in your example.jsp page:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:wrapper>
    <h1>Welcome</h1>
</t:wrapper>

That does exactly what you think it does.


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>

To use this:

<%@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>


But it turns you like to use that user detail block in other places. So, we'll refactor it. 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/>

Now the previous example becomes:

<%@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>


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 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).

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>

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.

So, tag files can be pretty much whatever you want them to be. At the most basic level, it's simple cut and paste refactoring. Grab a chunk of layout, cut it out, do some simple parameterization, and replace it with a tag invocation.

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

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

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆