在grails gsp模板中,如何在没有sitemesh抛出错误的情况下使用服务器端注释?

gab*_*abe 20 grails templates sitemesh gsp

当我在gsp模板中使用标准的jsp注释块时

<%-- some server-side comment --%>    
Run Code Online (Sandbox Code Playgroud)

,sitemesh引发'意外令牌'错误.我可以使用另一种评论语法吗?

Dón*_*nal 22

以下适用于我

%{-- <div>hello</div> --}%
Run Code Online (Sandbox Code Playgroud)


MAl*_*lex 17

你错过了'%'的标志.把它写成:

<%-- some server-side comment --%>
Run Code Online (Sandbox Code Playgroud)


iba*_*alf 5

最初的问题是询问如何注释掉 GSP 文件中的任何内容。唯一对我有用的是

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

其他答案将不起作用,特别是如果被注释的代码是 grails 标签。%{ 和 <% 不起作用。


Smi*_*eld 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>
Run Code Online (Sandbox Code Playgroud)

文件:_myTemplate.gsp

<h2>Template</h2>

<!-- visible -->
<% invisible %>
%{-- invisible --}%
<% /*invisible*/ %>
Run Code Online (Sandbox Code Playgroud)

(Grails 2.5.5)


gab*_*abe 4

常规的 java 注释块就可以工作

<% /*  some server side comment */ %>
Run Code Online (Sandbox Code Playgroud)

  • 这在 gsp 页面中不是一个好方法。使用真正为gsp页面创建的%{-- --}% (2认同)