25

When I use a standard jsp comment block in a gsp template

<%-- some server-side comment --%>    

, sitemesh throws an 'unexpected token' error. Is there another comment syntax I can use?

gabe
  • 1,127
  • 1
  • 11
  • 23

7 Answers7

26

The following works for me

%{-- <div>hello</div> --}%
Dónal
  • 185,044
  • 174
  • 569
  • 824
19

You are missing a '%' sign. Write it as :

<%-- some server-side comment --%>
MAlex
  • 1,234
  • 2
  • 16
  • 29
5

There's a little confusion among previous answers (and the question itself) that I wish was explained to me at first. There are a few types of server side comments on a .gsp. So within the .gsp document Server side comments go as follow:

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head></head>
<body>
    <!-- the basic HTML comment (not on server side) -->
    <h1>Visible on client side</h1>

    <%-- GSP common comment (server side only) --%>
    %{-- GSP alternative approach (again, on server side only) --}%
    <g:if test="${true}">
        <h1>Invisible on client side, just in source code</h1>
    </g:if>

    <p>and the one asked for happens elsewhere, 
    whenever you write classic Groovy script</p>
    <g:set var="myTitle"/>
    <%
        myVar = 'comment'
        if(myVar.equals('comment')){
            /*Needs the classic Java comment, 
            this happens whether you're writing a simple .gsp 
            or any _template.gsp*/
            myTitle = "<h1>Visible on server side only</h1>".encodeAsRaw()
        }
    %>
    ${myTitle}

    <p>.gsp template does not modify comment behaviour</p>
    <g:render template="/templates/myTemplate" model="[:]"/>
</body>
</html>

file: _myTemplate.gsp

<h2>Template</h2>

<!-- visible -->
<% invisible %>
%{-- invisible --}%
<% /*invisible*/ %>

(Grails 2.5.5)

Smithfield
  • 341
  • 4
  • 11
5

The original question was asking how to comment out anything in the GSP file. The only one that worked for me is

<%-- some code to comment out --%>,

the other answers won't work especially if the code being commented are grails tags. %{ and <% didn't work.

ibaralf
  • 12,218
  • 5
  • 47
  • 69
4

A regular java comment block will work

<% /*  some server side comment */ %>
gabe
  • 1,127
  • 1
  • 11
  • 23
0

if you are writing a gsp that wants to display a uninterpreted grails g: tag, e.g. you want <g:link ... to appear as-is on the page, without being interpreted server-side, the following worked nicely for me.

In both the start and end tags, replace the < with &lt;

e.g.

<g:link...>...</g:link> gets interpreted server-side and shows up in the page a link.

&lt;g:link ...>...&lt;/g:link ...> shows up in the front-end page as <g:link...>...</g:link>

nby
  • 119
  • 1
  • 3
0

<%-- server side code --%> should work

Tushar
  • 85,780
  • 21
  • 159
  • 179
Tony
  • 35
  • 4