当我在 gsp 模板中使用标准 jsp 注释块时
<%-- some server-side comment --%>
,sitemesh 抛出“意外令牌”错误。我可以使用另一种注释语法吗?
以下对我有用
%{-- <div>hello</div> --}%
您缺少一个 '%' 符号。写成:
<%-- some server-side comment --%>
最初的问题是询问如何注释掉 GSP 文件中的任何内容。唯一对我有用的是
<%-- some code to comment out --%>
,
其他答案将不起作用,特别是如果被注释的代码是 grails 标签。%{ 和 <% 不起作用。
我希望首先向我解释的先前答案(以及问题本身)之间存在一些混淆。.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*/ %>
(圣杯 2.5.5)
常规的 java 注释块将起作用
<% /* some server side comment */ %>
如果您正在编写一个想要显示未解释的 grails g:标记的 gsp,例如,您希望<g:link ... 按原样显示在页面上,而不是在服务器端进行解释,那么以下内容对我来说效果很好。
在开始和结束标记中,将<替换为<
例如
<g:link...>...</g:link>在服务器端得到解释并在页面中显示一个链接。
<g:link ...>...</g:link ...>在前端页面中显示为<g:link...>...</g:link>
<%-- server side code --%>
应该管用