David Sills's muse is fueled by Bach, Beethoven, and Brahms (and Berlioz and Boulez). He has no life, which comports well with the über-geekitude to which he aspires. He reads and writes fitfully in English, Spanish, French, German, Italian (mostly medieval), C, VB, Perl.... Oh, and Java and XSLT, lots and lots of Java and XSLT. Trained somewhere in darkest Central America, he works today at DataSource, Inc., on the product team for the CASE tool Abri. David has posted 9 posts at DZone. View Full User Profile

Running the Table With JMesa

06.18.2008
| 44480 views |
  • submit to reddit

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;
}

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest
request, HttpServletResponse response) throws Exception
{
Set<HelloWorld> results = helloWorldService.findAll();
TableFacade tableFacade = new TableFacadeImpl("results",request);
tableFacade.setItems(results);
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"%>
<tags:layout-top>
<jsp:attribute name="titleBar"><spring:message
code="title.bar.results"/></jsp:attribute>
<jsp:attribute name="titleInternal"><spring:message
code="internal.title.results"/></jsp:attribute>
</tags:layout-top>
<form name="resultsForm" action="search.html">
${results}
</form>
<script type="text/javascript">
function onInvokeAction(id, action)
{
setExportToLimit(id, '');
createHiddenInputFieldsForLimitAndSubmit(id);
}
function onInvokeExportAction(id, action)
{
location.href="${pageContext.request.contextPath}/search.html?"
+ createParameterStringForLimit(id);
}
</script>
<tags:layout-bottom/>

And we'll need to create and point to the new controller in jmesa-servlet.xml:

<bean id="helloWorldService"
class="com.javalobby.article.jmesa.entity.HelloWorldService"/>
<bean id="basicSearchController"
class="com.javalobby.article.jmesa.web.BasicJMesaSearchController">
<property name="helloWorldService" ref="helloWorldService"/>
</bean>
...
<prop key="/search.html">basicSearchController</prop>

Redeploy, and the results look like magic. How did we get them?

Figure 2.: Using JMesa "out of the box"Figure 2.: Using JMesa "out of the box"



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:

Figure 3.: JMesa search results sorted and highlightedFigure 3.: JMesa search results sorted and highlighted



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!

Figure 4.: JMesa search results filteredFigure 4.: JMesa search results filtered



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.

Legacy
Article Resources: 
Published at DZone with permission of its author, David Sills.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Nicolas Frankel replied on Wed, 2008/06/18 - 10:19am

Hello,

Don't get me wrong, but displaying tabular data in HTML is done since a loooooong time with the DisplayTaglib. There's a short article on my blog if you're interested. They use taglibs but I see it as an advantage.

For more informations, see their site or their live demos which are really bluffing when you see the tiny amount of config needed on the JSP.

Cheers.

Nicolas

 

 

Benoit Guerout replied on Wed, 2008/06/18 - 10:53am

I've a very bad recollection of the last time I've tried to create a table JMesa (old but wonderfull displaytag solved easly the problem).

But, may be, this tutorial will help me to succeed the next time i'll try JMesa.

Jeff Johnston replied on Wed, 2008/06/18 - 7:19pm

What a great article...I was very excited to read this today :)! It was very well thought out and complete. You really tapped into just how customizable JMesa is. By design, just about every aspect of JMesa can be tweaked and modified with very little code. I will for sure link to this article from the home page of JMesa and reference it in the tutorials!

I am going to see about implementing your extended attributes notion. I think that is a nice improvement! I also hope to make some adapters for Spring so that those that choose to use the Spring framework can tie into Spring's notion of messages and preferences. It wouldn't be hard to do and would be a completely optional runtime dependency so non-Spring developers would not be effected.

Nicolas, there is a complete tag library with JMesa. The tag library is just a thin wrapper around the API. In addition JMesa has first class Groovy support and, in the next release, will even have Grails support with GSP Tags. The latter is currently being built by a member of the JMesa community and I am very excited to see that work as well. I have seen other work by this developer and it is always top notch. Also, as the article mentions, JMesa can also be put in an editable mode and will track your changes automatically.

bguerout, sorry to hear your first go around wasn't so great. How long ago was that? Based on how few questions I get nowadays I can say with confidence that your next experience should be really good. It is a very solid API! If you have any problems either post on the groups or email me directly. I try to get back ASAP as I know how frustrating it is to wait for replies when there are questions.

I wanted to mention that JMesa was designed to be an API first and foremost. It also embraces the Model 2 environment. Both of these give it the advantage of being able to use JMesa with just about any framework and regardless of the view technology being used.

David, if you don't mind, I also wanted to mention a few subtle improvements to your example that may make things easier:

  • You can actually turn off filtering at the row level by setting filterable to false. This will cause the filter row to not be rendered and the filter toolbar items to not display.
  • You might want to consider extending AbstractCellEditor as that will give you access to the Column, as well as the CoreContext and WebContext.
  • You do not need to inject your CellEditor with the CoreContext or WebContext. The support interfaces are there so the API can do that automatically for you when you set the editor on the renderer.
  • You can also define bogus column names instead of null for the column properties that do not map to a bean attribute. That enables you to reference the columns by name instead of position. It seems I usually have columns like "chkbox" myself that do not actually map to bean properties.

Once again, congratulations on a great article!

-Jeff Johnston

 

David Sills replied on Sun, 2008/06/22 - 1:59am in response to: Nicolas Frankel

Nicolas:

Thanks for your comment and thanks for reading.

It was, in fact, using Display Taglib for a while that got me to looking around for something easier to customize and with a few more features. I agree completely that if it meets your needs, it's a terrific library. It simply didn't meet mine.

That said, chacun à son goût, as the French say. The beauty of the Java world today is that we have enough open-source code around that everyone can use whatever they feel comfortable with.

Cheers,

David Sills

David Sills replied on Sun, 2008/06/22 - 2:01am in response to: Jeff Johnston

Jeff:

Thanks to you for authoring JMesa! Without it, of course, the article would have been, shall we say, less interesting?

I have incorporated some of your comments into the code and resources. I would be pleased if you wanted to link to the article.

All the best! 

David Sills

Neil Juan replied on Fri, 2008/08/22 - 9:07pm

I guess were on same track, I'm using JMesa for quite a while, in my swf application. If I do have problems, could I ask for help?

David Sills replied on Sat, 2008/08/23 - 1:23pm in response to: Neil Juan

Sure, but your best bet is the Google JMesa group: http://groups.google.com/group/jmesa/topics. Jeff Johnston (the creator of JMesa) moderates, and is extremely friendly and helpful.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.