Andy, andyandy wrote:
I need to send some data to an external service that interacts with my application. It's just a group of ids and values that the service needs. The real code that I have is:#set ($query="id_maq=escape($!{idMaq})&referer=escape($!{referer})&urlv=escape($!{urlv})&urlf=escape($!{idMaq}$!{url})&serv=escape($!{serv})") The problem is that when any of the variables is null it tries to "escape(null)", which I don't want. I only want to escape the values that I do have.
You can do this in many ways.
1. Use iterative conditionals
#if($idMaq)
#set($query = "$query&id_maq=${idMaq})
#end
#if($referer)
#set($query = "${query}&referer=${referer})
#end
.
.
.
2. Write a macro to do the conditionals for you:
#macro(possiblyEscape $paramName $paramValue)
#if($paramValue)&${paramName}=${paramValue}
#end
(note that you might want to put this all on one line to avoid
excess whitespace)
($query = "#possiblyEscape('id_maq',
$idMaq)#possiblyEscape('referer', $referer) ... ")
I'm sure there are other options.Also note that you might want to run your parameter names and values through URLEncoder.encode. Consider using LinkTool from Velocity Tools, which handles lots of stuff for you like this.
-chris
signature.asc
Description: OpenPGP digital signature
