Monday, July 5, 2010

DisplayTag and SiteMesh

A friend of mine asked how to use DisplayTag in a Spring-powered webapp decorated with SiteMesh. DisplayTag has been around for a long time and its development has been stopped, but it's still a useful library if you want to quickly generate fancy tables in JSP. This short tutorial's objective is to make something like this:


I assume you're familiar with Java webapps and know to use Maven. Let's get started. First create a webapp project using Maven archetype:

mvn archetype:create -DgroupId=com.wiradikusuma.tutorial -DartifactId=displaytag-sitemesh-demo -DarchetypeArtifactId=maven-archetype-webapp

Edit the generated pom.xml by adding dependencies to Servlet API, JSP API, Spring MVC, SiteMesh and DisplayTag. We don't really need Spring for this very simple demo, but I include it anyway since most likely you will use it in your real webapps. The resulting pom.xml should look like this:


Don't forget to adjust your web.xml to include Spring and SiteMesh:


As for the table itself, here's the code:


name is the model name specified in IndexController. id is to specify variable name for each row (e.g. so you can do ${data.name} inside display:column). export means you want it to allow exporting. requestURI is required if your JSP file is not directly mapped to URL (e.g. stored inside WEB-INF).

You can download the self contained final project here. You can open it from Eclipse (with Maven plugin installed), IntelliJ or NetBeans IDE.

No comments:

Post a Comment