jQuery tmpl 和 JSP 代码不能很好地运行
我一直在一些项目中使用 jQuery tmpl 库,并且非常喜欢它。
我没有在需要 JSP 的小项目上使用它,事情变得很奇怪。它没有完全发挥作用。
<script id="servicesRow" type="text/x-jquery-tmpl">
<tr id="id_${id}">
<td>${name2}<br />${id}</td>
<td>${supported_roles}</td>
<td><button class="edit">Edit</button></td>
<td><button class="delete">Delete</button></td>
<td><a href="#">Show clients</a></td>
</tr>
</script>
我试图理解为什么没有显示数据。事实证明,页面中类似 ${foo}
的文本正在发生某种解析。因此,当我在页面上查看源代码时,所有这些元素都已被替换,如下所示:
<script id="servicesRow" type="text/x-jquery-tmpl">
<tr id="id_">
<td><br /></td>
<td></td>
<td><button class="edit">Edit</button></td>
<td><button class="delete">Delete</button></td>
<td><a href="#">Show clients</a></td>
</tr>
</script>
它仍然可以用作模板,但 jQuery tmpl 无法完成其工作。我得到很多空行。
该语法与我找到的 JSTL 的一些文档相匹配。但据我所知,我还没有安装它。我正在 Windows 7 上的现有 Tomcat 上进行开发,并从头开始在我的 webapps/ 文件夹中构建一个应用程序。据我所知,我没有启用类似的东西,而且我很惊讶裸露的 ${}
被解析(而不是更像 <%= % >
,这在 PHP 或 ASP 等中更为常见,
所以我的问题是:如何在全局、本地到单个 JSP 中关闭这种解析行为?我已经尝试过额外的大括号,我尝试过反斜杠,我尝试过各种引号)我认为理想情况下会是这样的:
<foo:stopParsingMyDollarSignsAndBracesPlease>
<script id="servicesRow" type="text/x-jquery-tmpl">
<tr id="id_${id}">
<td>${name2}<br />${id}</td>
<td>${supported_roles}</td>
<td><button class="edit">Edit</button></td>
<td><button class="delete">Delete</button></td>
<td><a href="#">Show clients</a></td>
</tr>
</script>
</foo:stopParsingMyDollarSignsAndBracesPlease>
任何帮助或想法谢谢!
I've been using the jQuery tmpl library for some projects and really liking it.
I'm not using it on a small project that needs to be in JSP, and things got strange. It is not working fully.
<script id="servicesRow" type="text/x-jquery-tmpl">
<tr id="id_${id}">
<td>${name2}<br />${id}</td>
<td>${supported_roles}</td>
<td><button class="edit">Edit</button></td>
<td><button class="delete">Delete</button></td>
<td><a href="#">Show clients</a></td>
</tr>
</script>
I was trying to understand why no data was showing up. Turns out some kind of parsing is happening of text in the page that looks like ${foo}
. So when I view source on my page all those elements have been replaced, like this:
<script id="servicesRow" type="text/x-jquery-tmpl">
<tr id="id_">
<td><br /></td>
<td></td>
<td><button class="edit">Edit</button></td>
<td><button class="delete">Delete</button></td>
<td><a href="#">Show clients</a></td>
</tr>
</script>
Which is still usable as a template, but then jQuery tmpl can't do its job. I get lots of empty rows.
The syntax matches some documentation I've found for JSTL. But I've not, that I can tell, installed that. I'm developing on stock, current Tomcat on Windows 7 and building up an app in my webapps/ folder from scratch. I've not, that I can tell, enabled anything like this, and I'm surprised that bare ${}
is getting parsed (as opposed to things more like <%= %>
, which would be more common as from PHP or ASP and similar.
So my question: how do I turn off this parsing behavior for my jQuery tmpl templates? Globally, locally to the individual JSP, or escape it (I've tried extra braces, I've tried backslashes, I've tried various quotes). I think Ideally there would be something like:
<foo:stopParsingMyDollarSignsAndBracesPlease>
<script id="servicesRow" type="text/x-jquery-tmpl">
<tr id="id_${id}">
<td>${name2}<br />${id}</td>
<td>${supported_roles}</td>
<td><button class="edit">Edit</button></td>
<td><button class="delete">Delete</button></td>
<td><a href="#">Show clients</a></td>
</tr>
</script>
</foo:stopParsingMyDollarSignsAndBracesPlease>
Any help or ideas are appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
${}
表示法是表达式语言。您可以通过以下方式在每个 JSP 的基础上关闭它,或者通过在
web.xml
中的以下内容在应用程序范围内关闭它:或者如果您确实想在 JSP 的其他地方使用 EL,那么您可以只需要通过
\
转义 jQuery 模板中的内容即可。您确实希望避免使用老式的scriptlet。或者您可以将所有 JS 代码放入其自己的 JS 文件中(如果 jQuery tmpl 支持)。
The
${}
notation is Expression Language. You can turn it off on a per-JSP basis byOr on an application-wide basis by the following in
web.xml
:Or if you actually would like to use EL elsewhere in the JSP, then you just have to escape the ones in jQuery template by
\
. You'd really like to avoid the old fashioned scriptlets.Or you could just drop all that JS code in its own JS file (if supported by jQuery tmpl).
在 jQuery 模板中使用
{{=variable_name}}
而不是${variable_name}
。https://github.com/jquery/jquery-tmpl/issues/56
Use
{{= variable_name}}
instead of${variable_name}
in jQuery templates.https://github.com/jquery/jquery-tmpl/issues/56
不建议关闭EL,当jsp页面有其他EL时,你如何处理,
你可以这样做:
注意:El中有一个空格,
Turn off EL is not advisable,when the jsp page have other el ,and how do you deal with it,
you can do it like this:
note:there is a space in the El,
您可以通过使用反斜杠引用来保护您的“$”免受 JSP 的影响。
在 JSP 中,“${}”构造用于引入表达式语言(“EL”)构造。例如,如果有一个名为“firstName”的请求属性,您可以通过以下方式获取名字:
在我看来,使用表达式语言比使用可怕的“scriptlet”语法(“<% “ 东西)。页面变得非常非常干净,而且调试起来也非常方便。我不得不怀疑那里有很多非常古老的 JSP 教学指南和教科书,因为 JSTL 和 EL 已经存在很多年了。
You can protect your "$" from JSP by quoting with a backslash.
In JSP, the "${}" construct is used to introduce an Expression Language ("EL") construct. For example, if there's a request attribute called "firstName", you can get the first name with:
Using the Expression Language is, in my opinion, about a billion times better than using that hideous "scriptlet" syntax (the "<%" stuff). Pages are much, much cleaner, and there's far lest cruft to debug. I have tot suspect that there are a whole lot of really old JSP instructional guides and textbooks floating around out there, because JSTL and the EL have been around for many years now.
如果您不想转义所有 jQuery 标记,您可以使用所有 jQuery 脚本构建一个单独的 JSP,并通过
以下方式维护此独特的 JSP 保护:
<%@page isELIgnored="true" %>
然后您可以在需要的地方包含此 JSP。
If you don't want to escape all your jQuery tags you can build a separated JSP with all your jQuery script and mantain this unique JSP protected with:
<%@page isELIgnored="true" %>
Then you can include this JSP where needed.