在Grails的GSP模板中,如何使用服务器端注释而不会导致Sitemesh报错?

25

当我在GSP模板中使用标准的JSP注释块时

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

sitemesh抛出了一个“意外的令牌”错误。我可以使用另一种注释语法吗?


澄清一下,<%-- 语法在 gsp 中有效,但在使用 <g:render 包含的 gsp 模板中无效。 - gabe
问题是关于Grails 1.3.7的。 - gabe
7个回答

26

以下内容对我有效

%{-- <div>hello</div> --}%

19
你缺少了一个 '%' 符号。将其写为:
<%-- some server-side comment --%>

谢谢。错别字只出现在问题中,而不是代码中,问题已经修正。 - gabe

5

之前的回答(包括问题本身)有些混淆,希望一开始就能对我进行解释。在.gsp上有几种服务器端注释类型。

因此,在.gsp文档中,服务器端注释如下:

<%@ 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>

文件:_myTemplate.gsp

<h2>Template</h2>

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

(Grails 2.5.5)


5
原始问题是询问如何在GSP文件中注释掉任何内容。我唯一适用的方法是使用<%-- some code to comment out --%>,其他答案不可行,特别是当要注释的代码为grails标签时。 %{和<% 不起作用。

4

一个普通的Java注释块将起作用

<% /*  some server side comment */ %>

2
在GSP页面中使用这种方式并不是一个好的方法。 请使用为GSP页面特别创建的%{-- --}%标记。 - Muhammad Aamir Talib

0

<%-- 服务器端代码 --%> 应该可以工作


0

如果你在编写一个 GSP 页面,想要显示未解释的 Grails g: 标签,例如你想让 <g:link ... 在页面上呈现为原样,而不被服务器解释,下面的方法对我来说非常有效。

在开始和结束标记中,将 < 替换为 &lt;

例如:

<g:link...>...</g:link> 在服务器端进行解释并显示为链接。

&lt;g:link ...>...&lt;/g:link ...> 以前端页面形式呈现为<g:link...>...</g:link>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接