daily pastebin goal


a guest Dec 15th, 2011 30 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Creating JSP Layouts with Page Tags
  2. Creating JSP Layouts with Page Tags
  3. March 3rd, 2007 fforw Leave a comment Go to comments
  5. For some time now I’ve been following a new approach when creating the basic layout structure for JSP based web applications. The new approach is based on JSP 2.0 tag files and changes the way that the single pages are build up from reusable parts.
  7. The most common way to do so is to use included files with common blocks:
  9. <%@include “head.inc”%>
  11. <b>page specific markup</b>
  13. <%@include “tail.inc”%>
  15. Here the individual pages contain two or more includes which define the page start and end. Often navigation or sidebars or whatever are included in their own files. Changing the output of the title in the header e.g. requires passing data through the page scope or so.
  17. My new approach defines the basic layout as JSP 2.0 tag file. Let’s take a look at an example:
  19. <%@tag description=”page layout” %>
  20. <%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>
  21. <%@attribute name=”head” fragment=”true”%>
  22. <%@attribute name=”title” required=”true” type=”java.lang.String”%>
  23. <%@attribute name=”script” required=”false” type=”java.lang.String”%>
  24. <%@attribute name=”stylesheet” required=”false” type=”java.lang.String”%>
  25. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
  26. <html>
  27. <head>
  28. <title>${title} – Page Tag Example</title>
  29. <style type=”text/css”>
  30. @import url(“<c:url value=”/style/style.css”/>“);
  31. <c:forTokens var=”item” items=”${stylesheet}” delims=”,”>
  32. @import url(“<c:url value=”/style/”/>${item}“);
  33. </c:forTokens>
  34. </style>
  36. <c:forTokens var=”item” items=”${script}” delims=”,”>
  37. <script type=”text/javascript” src=”<c:url value=”/script/”/>${item}“>
  38. </script>
  39. </c:forTokens>
  40. <jsp:invoke fragment=”head” />
  41. <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
  42. <meta http-equiv=”Pragma” content=”no-cache”>
  43. </head>
  44. <body>
  45. <h1>Page Tag Example</h1>
  46. <ul id=”nav”>
  47. <li><a href=”index.jsp”>Index</a></li>
  48. <li><a href=”page1.jsp”>Page 1</a></li>
  49. <li><a href=”page2.jsp”>Page 2</a></li>
  50. </ul>
  51. <div id=”content”>
  52. <jsp:doBody />
  53. </div>
  54. <hr>
  55. <div id=”footer”>
  56. <a href=”http://validator.w3.org/check?uri=referer”><img
  57. src=”http://www.w3.org/Icons/valid-html401″ border=”0″
  58. alt=”Valid HTML 4.01 Transitional” height=”31″ width=”8″></a>
  59. </div>
  60. </body>
  61. </html>
  63. The tag defines the example page layout with a page header, a navigation and a footer. There are several attributes to control the page tag from inside the JSP page it is used in:
  65.     head – Fragment attribute used to add additional HTML markup to the head section of the page
  66.     title – Required attribute which sets the page specific part of the page title
  67.     script – Comma separated lists of script files to include from the /script directory
  68.     stylesheet – Comma separated lists of style sheets to import from the /style directory
  70. The head fragment attribute demonstrates how to setup a fragment attribute which makes it possible to set HTML code as an attribute. To use such a fragment attribute inside a page you need to use the <jsp:attribute> and <jsp:body> tags:
  72. <%@taglib tagdir=”/WEB-INF/tags” prefix=”example”%>
  73. <example:page title=”Page 1″>
  74. <jsp:attribute name=”head”>
  75. <meta name=”author” content=”Sven Helmberger”>
  76. </jsp:attribute>
  77. <jsp:body>
  78. Page 1 – demonstrates the use of the head fragment attribute
  79. </jsp:body>
  80. </example:page>
  82. If you only use normal (non-fragment) attributes you can leave out the <jsp:attribute> and <jsp:body> tags and specify the rest of the attributes like the title attribute above. Fragment attributes can also be useful to define common code blocks besides the tag body like a page-specific area in a sidebar or so.
  84. You can see that, even with fragment attributes complicating things, the page tag leads to a simple JSP code with high reuse and clear passing of data from the special page code to the common code.
RAW Paste Data