在 grails gsp 模板中,如何使用服务器端注释而不使站点网格抛出错误?
Posted
技术标签:
【中文标题】在 grails gsp 模板中,如何使用服务器端注释而不使站点网格抛出错误?【英文标题】:In a grails gsp template, how can I use a server side comment without sitemesh throwing an error? 【发布时间】:2011-09-29 20:59:27 【问题描述】:当我在 gsp 模板中使用标准 jsp 注释块时
<%-- some server-side comment --%>
,sitemesh 抛出“意外令牌”错误。我可以使用其他注释语法吗?
【问题讨论】:
澄清一下, 中的 gsp 模板 问题是针对 Grails 1.3.7 【参考方案1】:以下对我有用
%-- <div>hello</div> --%
【讨论】:
【参考方案2】:您缺少一个“%”符号。写成:
<%-- some server-side comment --%>
【讨论】:
谢谢。错字只是在问题中,而不是代码,问题已解决。【参考方案3】:最初的问题是询问如何注释掉 GSP 文件中的任何内容。唯一对我有用的是
<%-- some code to comment out --%>
,
其他答案将不起作用,特别是如果被注释的代码是 grails 标签。 % 和
【讨论】:
【参考方案4】:之前的答案(以及问题本身)之间存在一些混淆,我希望首先向我解释。 .gsp 上有几种类型的服务器端 cmets。 因此,在 .gsp 文档中,服务器端 cmets 如下所示:
<%@ 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】:一个常规的 java 注释块可以工作
<% /* some server side comment */ %>
【讨论】:
它在 gsp 页面中不是很好的方法。使用真正为 gsp 页面创建的 %-- --%【参考方案6】:如果您正在编写一个 gsp 并希望显示未解释的 grails g: 标签,例如您希望
在开始和结束标记中,将 替换为 <
例如
... 在服务器端得到解释并在页面中显示一个链接。
<g:link ...>...</g:link ...> 在前端页面中显示为
【讨论】:
【参考方案7】:<%-- server side code --%>
应该可以工作
【讨论】:
以上是关于在 grails gsp 模板中,如何使用服务器端注释而不使站点网格抛出错误?的主要内容,如果未能解决你的问题,请参考以下文章
Grails 5.0.1 - 自定义脚手架 edit.gsp 模板和其他
如何在 grails messages.properties 中使用 HTML 进行邮件发送