当我在GSP模板中使用标准的JSP注释块时
<%-- some server-side comment --%>
sitemesh抛出了一个“意外的令牌”错误。我可以使用另一种注释语法吗?
以下内容对我有效
%{-- <div>hello</div> --}%
<%-- some server-side comment --%>
之前的回答(包括问题本身)有些混淆,希望一开始就能对我进行解释。在.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)
<%-- some code to comment out --%>
,其他答案不可行,特别是当要注释的代码为grails标签时。 %{和<% 不起作用。一个普通的Java注释块将起作用
<% /* some server side comment */ %>
<%-- 服务器端代码 --%>
应该可以工作
如果你在编写一个 GSP 页面,想要显示未解释的 Grails g: 标签,例如你想让 <g:link ... 在页面上呈现为原样,而不被服务器解释,下面的方法对我来说非常有效。
在开始和结束标记中,将 < 替换为 <
例如:
<g:link...>...</g:link> 在服务器端进行解释并显示为链接。
<g:link ...>...</g:link ...> 以前端页面形式呈现为<g:link...>...</g:link>