Mar 05

Those who can not only been with Grails Web applications based on Java, employs the expected JSP taglib DisplayTag be a household name.

DisplayTag it without big chins tables in Java server pages into multiple pages (paging), sort, group, and export to Excel, csv, xml and pdf.
Grails was supported "out of the box" only the paging and long time we do not use JSP tag libs in Grails, so you could map the remaining functionality either through plugins or even had to lend a hand.

Since Grails 1.1, this restriction is now lifted and combine the two, the test has long been successfully completed. Only the blog entry about it is, since even on my ToDo list. But what good will come to those (I hope :-) ).

The following steps are necessary to integrate DisplayTag in Grails:

libs

First, libraries should be DisplayTag next to the database driver copied to the lib directory.

DisplayTag configuration file

The configuration file of DisplayTag displaytag.properties be under src/java to be created. The structure of this file is in the document described by DisplayTag. For my grum project, the configuration looks like this:

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
  csv excel xml pdf export. csv excel xml pdf types =
 true export. excel = true
 true export. csv = true
 true export. xml = true
 true export. pdf = true
 = org. displaytag . export . excel . DefaultHssfExportView export. excel. class = org. DisplayTag. export. excel. DefaultHssfExportView
 = org. displaytag . export . DefaultPdfExportView export. pdf. class = org. DisplayTag. export. DefaultPdfExportView
 = org. displaytag . export . DefaultRtfExportView export. rtf. class = org. DisplayTag. export. DefaultRtfExportView
 div class = "exportlinks" > Export to : { 0 } </ div > export banner = <div class = "export-left"> Export to. {0} </ div>
 list export. amount = list
 = bottom paging. banner. placement = bottom
 = No data found. basic. msg. empty_list = No data found.
 . empty_list_row =< tr class = "empty" >< td colspan = "0" > No data found. </ td ></ tr ></ tr > basic. msg. empty_list_row = <tr class = "empty"> <td colspan = "0"> No data found. </ td> </ tr> </ tr>
 =< span class = "pagelinks" >< a href = "{1}" >< strong >& lt ;& lt ;</ strong ></ a >& nbsp ;& nbsp ;& nbsp ;< a href = "{2}" >< strong >& lt ;</ strong ></ a >& nbsp ; { 0 } & nbsp ;< a href = "{3}" >< strong >& gt ;</ strong ></ a >& nbsp ;& nbsp ;& nbsp ;< a href = "{4}" >< strong >& gt ;& gt ;</ strong ></ a ></ span > paging banner full = <span class = "page left"> <a href = "{1}"> <strong> & lt;.. & lt; </ strong> </ a> & nbsp; & nbsp; & nbsp; <a href = "{2}"> <strong> & lt; gt <a href = "{3}"> <strong> &;; </ strong> </ a> & nbsp; {0} & nbsp < / strong> </ a> & nbsp; & nbsp; & nbsp; <a href = gt "{4}"> <strong> &; & gt; </ strong> </ a> </ span>
 =< span class = "pagelinks" > { 0 } & nbsp ;< a href = "{3}" >< strong >& gt ;</ strong ></ a >& nbsp ;& nbsp ;& nbsp ;< a href = "{4}" >< strong >& gt ;& gt ;</ strong ></ a ></ span > paging banner first = <span class = "page left"> {0} & nbsp;.. <a href = "{3}"> <strong> & gt; </ strong> </ a> & nbsp; & nbsp; & nbsp; <a href = "{4}"> <strong> & gt; & gt; </ strong> </ a> </ span>
 =< span class = "pagelinks" >< a href = "{1}" >< strong >& lt ;& lt ;</ strong ></ a >& nbsp ;& nbsp ;& nbsp ;< a href = "{2}" >< strong >& lt ;</ strong ></ a >& nbsp ; { 0 } </ span > paging banner last = <span class = "page left"> <a href = "{1}"> <strong> & lt;.. & lt; </ strong> </ a> & nbsp; & nbsp; & nbsp; <a href = "{2}"> <strong> & lt; </ strong> </ a> & nbsp; {0} </ span>
 =< span class = "pagebanner" > 1 Record found. Seite : </ span > paging banner one_item_found = <span class = "banner page"> page 1 record found... </ span>
 =< span class = "pagebanner" > { 0 } Records found. Seite : </ span > paging banner all_items_found = <span class = "banner page"> {0} records found Page:... </ span>
 =< span class = "pagebanner" > { 0 } Records found, show { 2 } - { 3 } . </ span >< br />< br /> paging banner some_items_found = <span class = "banner page"> {0} records found, show {2} -... {3} </ span> <br /> <br /> 

DisplayTag wrapper

Dynamic links and formatting to be implemented over the Decorator pattern in DisplayTag. The decorator is implemented in a wrapper class that is of course written in Groovy in Grails. My implementation is the class DisplaytagWrapper who created the project grum a link to the details of the user and formats the date of creation.

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
  package de.koo.grum.groovy.util

 ; org.displaytag.decorator.TableDecorator import;

 TableDecorator { public class extends DisplaytagWrapper TableDecorator {
    String getLinkToUser () {
       def user = getCurrentRowObject ()
       /grum/user/show/${user.id} \" >${user.username}</a>" ; return "<a href= \" /grum/user/show/${user.id} \"> $ {user.username} </ a>";
    }
    String getUserCreatedAt () {
       def user = getCurrentRowObject ()
       . format ( "dd.MM.yyyy HH:mm" ) ; user createdAt return format ("yyyy HH: mm")..
    }
 } 

Export filters

Be set when exporting to a non-HTML format, the content type for the response to the browser. To prevent this from causing problems, a filter should be defined in the web.xml file, which counteracts the possible errors. DisplayTag brings the class override filter response is already an implementation for it with.

To Grails in changes to the web.xml to make you, the templates must be installed:

  grails install-templates 

Then there is the web.xml in the directory src/templates/war . To filter the following entries are necessary.

  A
 2
 3
 4
 5
 6
 7
 8
  <filter>
    <filter-name> Override Response Filter </ filter-name>
    <filter-class> org.displaytag.filter.ResponseOverrideFilter </ filter-class>
 </ Filter>
 <filter-mapping>
    <filter-name> Override Response Filter </ filter-name>
    <url-pattern> / * </ url-pattern>
 </ Filter-mapping> 

Integration with Groovy Server Page

If you have completed all these preparatory steps, you can finally get down to using DisplayTag in a Groovy Server Page.
In my example, user / list.gsp all user data will be displayed in tabular form. The table is by the attribute sortable sortable by first and last name (without a new database access) and, when the pagesize of the attribute partialList spread over several pages.

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
  = "display" % > <% @ Taglib uri = "http://displaytag.sf.net" prefix = "display"%>  
 <html>
 ..
 defaultsort = "1" partialList = "true" <Display: table name = "user list" defaultsort = "1" partialList = "true"
 decorator = "de.koo.grum.groovy.util.DisplaytagWrapper"
 = "${pageSize}" requestURI = "/grum/user/list" size = "$ {size} result" pageSize = "$ {pagesize}" requestUri = "/ grum / user / list"
 = "0" cellspacing = "0" sort = "list" > class = "listing" cellpadding = "0" cellspacing = "0" sort = "list">
    title = "Userame" /> <display:column property = title = "linkToUser" "Userame" />						
    title = "Firstame" sortable = "true" /> <display:column property = title = "firstname" "Firstame" sortable = "true" />						
    title = "Lastname" sortable = "true" /> <display:column property = title = "lastname" "Lastname" sortable = "true" />						
    title = "Created At" sortable = "true" /> <display:column property = title = "userCreatedAt" "Created At" sortable = "true" />						
 </ Display: table>
 ... 

Additional attributes ( export , group ) may, as in the example contact data / list.gsp also export to different output formats and an array of entries are reached.

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
  export = "true" defaultsort = "1" partialList = "true" <Display: table name = "contact list data" export = "true" defaultsort = "1" partialList = "true"
 decorator = "de.koo.grum.groovy.util.DisplaytagWrapper"
 = "${pageSize}" requestURI = "/grum/contactData/list" size = "$ {size} result" pageSize = "$ {pagesize}" requestUri = "/ grum / contact data / list"
 = "0" cellspacing = "0" sort = "list" > class = "listing" cellpadding = "0" cellspacing = "0" sort = "list">
     value = "contactData.csv" /> name = value = <display:setProperty "export.csv.filename" "contactData.csv" />
     value = "contactData.xls" /> name = value = <display:setProperty "export.excel.filename" "contactData.xls" />
     value = "contactData.xml" /> name = value = <display:setProperty "export.xml.filename" "contactData.xml" />
     value = "contactData.pdf" /> name = value = <display:setProperty "export.pdf.filename" "contactData.pdf" />
     value = "list" /> name = value = <display:setProperty "export.amount" "list" />

     title = "Name" sortable = "true" group = "1" /> <display:column property = title = "linkContactDataToUser" "Name" sortable = group = "true" "1" />						
     title = "type" sortable = "true" /> <display:column property = title = "type" "type" sortable = "true" />
     sortable = "true" /> <display:column property = "data" sortable = "true" />
 </ Display: table> 

Conclusion:
Once configured DisplayTag one of the greatest solutions for Java-based Web applications to be listed in tabular data. The functionality far surpasses the standard functionality of Grails. The combination of Grails and DisplayTag I will recommend this to anyone.

Links:
grum source

gklinkmann written by \ \ tags: , , ,

Add a comment

Yes, I would like to be notified about comments!