Search code examples
javajspspring-mvcjakarta-eedisplaytag

How to modifiy the empty value of a column in displaytag?


If a column's value is null, DisplayTag prints an empty String. Here is the source code doing that in the org.displaytag.model.Column#getValue method :

if (object == null || "null".equals(object)) //$NON-NLS-1$
{
    if (!this.header.getShowNulls())
    {
        object = TagConstants.EMPTY_STRING;
    }
}

I'm wondering if there is a way to override that and display a specific value rather than empty String. What I'm looking for is a generic/automatic solution because otherwise I could handle that manually by testing if my attribute corresponding to the column is null and returning the specific character if needed...


Solution

  • You can use a Decorator to customize the value of any column as required:

    Examples

    http://demo.displaytag.org/displaytag-examples-1.2/example-decorator.jsp

    Tag Reference:

    http://www.displaytag.org/10/tagreference-displaytag-12.html

    Thus create a class:

    public class MyCustomColumnDecorator implements DisplaytagColumnDecorator{
    
        public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media) {
            return value == null ? "some string" : value;
        }
    }
    

    Specify this in your markup for any column you wish to use it for:

    <display:table name="someList">
      <display:column sortable="true" title="ID"/>
      <display:column property="email" autolink="true"/>
      <display:column property="description" title="Comments" decorator="com.test.MyCustomColumnDecorator"/>
    </display:table>