2 instances of the same table inside jsp

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?

Asked by: Abigail446 | Posted: 23-01-2022

Answer 1

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.

Answered by: Connie202 | Posted: 24-02-2022

Answer 2

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:

    <table id="results"> ... </table>

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.


<%@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"%>    
        <title>Page Title</title>
        <tag:page_nav startPage="${startPage}" endPage="${endPage}"/>
        <tag:page_nav startPage="${startPage}" endPage="${endPage}"/>


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

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.

Answered by: Melissa690 | Posted: 24-02-2022

Similar questions

java - creating instances of classes

I am currently working on some code with which I am having some trouble. I have two buttons on a GUI. If one button is pressed it assigns a value to a string a value to reflect this. The other button is the same except that a different value is assigned to the string. This String is created at the beginning before the constructor in the following manner: public string s = "String"...

How can we open multiple IE instances while testing web appl. with selenium RC and java in eclipse?

I am running a test in Selenium RC with Java in Eclipse to browse two URLs. I get two Selenium windows, but testing of both URLs is done by opening one single IE window. How can I make it open the URLs on two different IE browsers and if possible, simultaneously?

java - Why would you "store instances of the class in the database as entities"?

I'm trying to understand a line from the Google Datastore API which says: JDO uses annotations on Java classes to describe how instances of the class are stored in the datastore as entities, and how entities are recreated as instances when retrieved from the datastore.

binary - Can I execute multiple instances of a Java class file?

I am thinking of executing multiple instances of same java binary (a socket application) with different configuration files (As a command line parameter). Does the JVM correctly handles this situation? In other words If I haven't any common resources (Files that can be locked etc.) will this approach make any problems? If so what are things that I need to be careful.

java - How is it possible to limit the number of instances of an EJB?

I wan to check if my stateful bean is passivated/activated and the corresponding callbacks are called properly. For that I want to configure the containers GlassFish and/or JBOSS to limit the number of instances of the bean. Is it possible ? If yes, how ?

java - Caching of instances

I am just curious about the java memory model a little. Here is what i though. If i have the following class public class Test { int[] numbers = new int[Integer.MAX_VALUE]; // kids dont try this at home void increment(int ind){ numbers[ind]++; } int get(int ind){ return numbers[ind]; } } There are multiple readers get() and one wri...

java - How can I share memory between two JVM instances?

I build a huge graph in JVM (Scala) which I want to use repeatedly, tweaking algorithms. I'd rather not reload it each time from disk. Is there a way to have it sit in one JVM while connecting from another, where the algorithms are being developed?

java - Pattern to create class instances by integer id?

I am reading a Stream, which provides an identifier (a simple int). Depending on the int different data follows, which i need to turn into objects. So far i created classes for each object type, and each class provides a read(InputStream input)-method which reads whatever data there is to be read for that kind of object (all object classes inherit from a common base class). However, there are numerous id's and thus num...

java - two instances of the same editor

I created an editor using the eclipse PDE. Every time I double click on the same file it opens a new instance of my editor instead of just selecting the one that already opened (like in .java files). My Editor input implements IEditorInput. How can I change it?

java - How can I get a count of the instances of an arbitrary class?

Given a class (ex: foo.bar.MyClass), how can I get a count of the number of instances of that class existing within the JVM? Thanks EDIT: I'm looking for the code that will retrieve this count. EDIT: More specifically, an implementation of the method Integer getInstanceCount(Class c)

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)