创建JSP布局模板的最佳方法是什么? [英] What is the best way to create JSP layout template?

查看:96
本文介绍了创建JSP布局模板的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JSP技巧使模板更容易?

I我是JSPs和Servlets的新手,我想知道是否有一种简洁的方法来创建一个布局jsp并在类似的jsp页面上重用它,比如asp.net母版页。

I'm new to JSPs and Servlets, I'm wondering is there a neat way to create a layout jsp and reuse it on similar jsp pages, something like asp.net master pages.

我用Google搜索,有人说使用模板
http:// java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates
使用jstl标记库。它说要放置这样的标签:

I googled it, some people say use templates http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates that uses jstl tag library. It says to put a tag like this:

<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>

但是我收到错误(因为jstl.jar和standard.jar在WEB-INF / lib /中目录)。

but I get error (because jstl.jar and standard.jar are in WEB-INF/lib/ directory).

然而有人说jstl模板根据这个
Struts OR Tiles OR ??? ...... JSP模板解决方案

However some say jstl template have problems according to this Struts OR Tiles OR ???...... JSP template solution

我很乐意帮助我了解最好的方法。

I would be glad to help me know the best way.

编辑:我需要的是将页面的布局拆分成内容,标题, ...并在使用布局模板的页面中设置此部件,与asp.net母版页完全相同。

What I need is to split page's layout into parts like content, header,... and set this parts in the page that uses the layout template, exactly like asp.net master page.

推荐答案

Put WEB-INF / tags / genericpage.tag中的以下内容

Put the following in 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>

这完全符合你的想法!

这是Will Hartung关于此链接的精彩答案的一部分。

This was part of a great answer by Will Hartung on this link.

这篇关于创建JSP布局模板的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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