Reference Manual for Gracelets - 2.0.0.RC2SourceForge.net Logo
2.2 - Tutorial : Using print() and println()
In Gracelets, print() and println() groovy commands direct output into a buffer that will be used to create text JSF UIComponents that will take care of rendering plain text (and EL or Closure expressions). The main difference between print() and println() is that println() will append the HTML <br/> tags as a replacement for each CRLF. This is very helpful when you use the multiline printing capabilities of groovy.
The following two examples will illustrate this:
Using print()
Toggle Line Numbers
1 print """ 
2     This is a print statement 
3     that is using multiline groovy syntax, 
4     but the CRLF's will not be translated to &lt;br/&gt; tags. 
5 """ 
http://somesite/somepage.jsf - Mozilla Firefox
http://somesite/somepage.jsf
http://somesite/somepage.jsf
This is a print statement that is using multiline groovy syntax, but the CRLF's will not be translated to <br/> tags.
Using println()
Toggle Line Numbers
1 println """ 
2     This is a print statement 
3     that is using multiline groovy syntax, 
4     and the CRLF's will be translated to &lt;br/&gt; tags. 
5 """ 
http://somesite/somepage.jsf - Mozilla Firefox
http://somesite/somepage.jsf
http://somesite/somepage.jsf

This is a print statement
that is using multiline groovy syntax,
and the CRLF's will be translated to <br/> tags.

When you do not pass EL or Closure expressions to the print statements, the output will ALWAYS be static, it will never change. Thus if you want dynamic content in your print statements, you need to either use inline EL expressions or pass a closure to the print and whatever that closure returns is what will be printed.

To understand the difference, notice the following two examples:

Using static print
Toggle Line Numbers
1 print new Date() 
http://somesite/somepage.jsf - Mozilla Firefox
http://somesite/somepage.jsf
http://somesite/somepage.jsf
Thu Aug 27 20:19:59 CDT 2009
If we refresh the page this first example will always print the same date.

Using print with closure expression
Toggle Line Numbers
1 print { Thread.sleep(2000); new Date(); } 
http://somesite/somepage.jsf - Mozilla Firefox
http://somesite/somepage.jsf
http://somesite/somepage.jsf
Thu Aug 27 20:28:04 CDT 2009
But with this last example, each page refresh will print the current date.

Conclusions about print() and println()

Thus if you simply want to print out text you can use simple print() or println() commands anywhere in your gracelet page. If you need dynamic content printed, you can use inline EL expressions, or even more powerful closure expressions.