tags:

views:

415

answers:

2

Currently there are 2 pages in a jsp file: one of them displays the data and the second one is used for pagination. The task is to include exactly the same paginator table above the data table. Sorry, couldn't resist to draw it :)

|-----------------------------------------|
|                      Page 2 of 200  < > |
|-----------------------------------------|
|-----------------------------------------|
|   Some weird business data comes here   |
|-----------------------------------------|
|-----------------------------------------|
|                      Page 2 of 200  < > |
|-----------------------------------------|

The question is: how do I do it w/o shameless copypasting?

+3  A: 

Perhaps you can define the pagination stuff in a separate jsp, and then include it twice into your main jsp. For example:

<jsp:include page="pagination.jsp" flush="true" />

<table>...business data...</table>

<jsp:include page="pagination.jsp" flush="true" />

This way, if you ever want to change the pagination stuff, you can just edit pagination.jsp.

pkaeding
yep. this sounds good to me.
Vinnie
+2  A: 

The four mechanisms of abstracting within JSP today are the jsp:include tag, the <%@ include> directive, custom tag libraries, and custom tag files.

jsp:include inserts the results of executing another JSP page, so you could do:

<jsp:include "page_naviagtor.jsp"/>
<table id="results">...</table>
<jsp:include "page_navigator.jsp"/>

<%@ include> is similar to jsp:include, save that it does not actually execute the code, rather it simply stamps it in to the original JSP source and is compiled with the rest of the page.

Custom tag libraries give you (almost) the full power of JSP tags, so you can do something like:

<tag:wrap_in_page_nav>
    <table id="results"> ... </table>
</tag:wrap_in_page_nav>

These require you to write custom Java code, however.

The final, and frankly, the best option for most cases, is the JSP 2.0 Tag FIle.

Tag Files are a cross between jsp:include and custom tags. They let you do something akin to the "wrap_in_page_nav" tag, but you actually create the tag use JSP markup.

So, in many cases, you can simply cut out the part you want to refactor, and paste it in to a Tag File, then simply use the tag.

page.tag

<%@tag description="put the tag description here" pageEncoding="UTF-8"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/yourtags" %>    
<%@attribute name="startPage" required="true"%>
<%@attribute name="endPage" required="true"%>    
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <tag:page_nav startPage="${startPage}" endPage="${endPage}"/>
        <jsp:doBody/>
        <tag:page_nav startPage="${startPage}" endPage="${endPage}"/>
    </body>
</html>

page_nav.tag

<%@tag description="put the tag description here" pageEncoding="UTF-8"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/yourtags" %>    
<%@attribute name="startPage" required="true"%>
<%@attribute name="endPage" required="true"%>    
<div>${startPage} .. ${endPage}</div>

Finally, your JSP

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/yourtags" %>
<tag:page startPage="1" endPage="4">
    <table> ... </table>
</tag:page>

Each of the tag files has the full power of JSP, the only limitation is that when using your own custom tag file, you can not include scriptlet code between your custom tag file tags (you can with normal JSP tags, just now tag file tags).

Tag Files are a very powerful abstraction tool for use within JSP.

Will Hartung
I've got away with <%@ include>
alex