Grails GSP concatenation nuance

 

by Taras Matyashovsky

 

While working with GSP it is a common situation to perform actions with strings, domain objects or other classes. Recently I faced the simple situation where I needed to dynamically build full file name (with file extension) in order to allow user to download it from file system. In my simple GSP I created the code similar to listed below:

 

<g:set var="documentFileFullName" value="${document.fileName + "." + document.fileType.name().toLowerCase()}/">

 

I use concatenation of strings in order to build full file name. But at runtime exception occurred which was very strange and gave me no tip for possible mistake made. As I discovered later in my example You can not use dot in double quotes (".") cause Grails wants to interprete expression in them and fails.

Therefore You can solve mentioned situation in two ways:

Use dot in single quotes ('.'). In that case correct solution might be:

 

<g:set var="documentFileFullName" value="${document.fileName + '.' + document.fileType.name().toLowerCase()}/">

 

 

Move mentioned logic to some separate class (for instance, let's call it DocumentFileNameBuilder) and call it as it listed below:

 

<%@ page import="com.company.project.core.ui.url.DocumentFileNameBuilder" %><g:set var="documentFileFullName" value="${new DocumentFileNameBuilder().build(document)}"/>

 

Simple stupid issue solved :) 

 
 
 

Comments  

 
0 # test 2009-12-02 08:13 tr Reply | Reply with quote | Quote
 
 
0 # test 2010-03-10 06:55 Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh