Running the Table With JMesa
Let's start with something extremely simple. On the very first page of the JMesa web site we find four lines of code that we can appropriate and refashion for a Spring controller:
public class BasicJMesaSearchController extends AbstractController
private HelloWorldService helloWorldService;
public void setHelloWorldService(HelloWorldService helloWorldService)
this.helloWorldService = helloWorldService;
protected ModelAndView handleRequestInternal(HttpServletRequest
request, HttpServletResponse response) throws Exception
Set<HelloWorld> results = helloWorldService.findAll();
TableFacade tableFacade = new TableFacadeImpl("results",request);
tableFacade.setColumnProperties("pk", "firstName", "lastName", "format");
return new ModelAndView("results", "results", tableFacade.render());
We let Spring inject the HelloWorldService, which we use to retrieve a set of items to display. Then we create and configure the JMesa TableFacade class. This class takes an HTTP request in its constructor: TableFacade is going to send itself messages passed as parameters in the request (more on this in a moment). We supply it with the set of items and with which JavaBean property of those items we want displayed in each column.
We'll also need a bit of new code in the search results page (in the project, this is actually a different search results page, as you, oh sharp-eyed reader, have already noticed):
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<form name="resultsForm" action="search.html">
function onInvokeAction(id, action)
function onInvokeExportAction(id, action)
And we'll need to create and point to the new controller in jmesa-servlet.xml:
<property name="helloWorldService" ref="helloWorldService"/>
Redeploy, and the results look like magic. How did we get them?
The key is in the variable results, which now holds the entire text of the table generated by the JMesa TableFacade when we called its render method. We also put a self-submitting HTML form around the JMesa table that it will use to send itself messages about how to alter itself.
This makes possible many amazing features. The table automagically paginates itself. It allows the user to change the number of rows displayed. It allows sorting on any column or combination of columns. It provides color striping of table rows and onMouseOver row highlighting. And every bit of this came for free: we did nothing to enable it but what you have already seen. (OK, we played around with some of JMesa's images and CSS style sheets to make it fit in with our color scheme, but that really shouldn't count.)
To demonstrate, we'll use the select at the top of the form to change the number of rows displayed to 16, sort by first name ascending and last name descending (by clicking on the first column header once and the second twice), and mouse over the third row to see the highlighting:
Now Al Gore and Einstein appear in the order we asked for.
You will have noticed the images in the table toolbar. Those on the left are standard first, previous, next, and last navigation icons. The select we've already mentioned. But there are two other images as well: these turn filtering, another amazing feature of JMesa that is active by default, on and off.
Filtering allows the user to apply expressions to a column in order to display only rows having matching values in that column. While filtering can take setup beyond the scope of this article, even by default it's astonishing. Try typing "Einstein" in the text field that appears above the last-name column header and clicking on the filter icon (the magnifying glass). The results show only the row containing Einstein's name in the last name column. And we didn't have to do a thing!
See the JMesa web site for details about filtering, editable tables that keep track of your changes for you, and much, much more: it's impressive stuff.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)