Velocity - 使用 jQuery 时如何避免 ParseErrorException?
我正在尝试将 jQuery 帖子添加到网页上的某些 JavaScript 中。整个页面由多个 Velocity 模板组成。一切都很好,直到我尝试添加 jQuery post,现在我得到:
org.apache.velocity.exception.ParseErrorException: Encountered "," at line 282, column 24 of /WEB-INF/velocity/www/comments.vm
Was expecting one of:
"(" ...
<RPAREN> ...
<ESCAPE_DIRECTIVE> ...
~~~snip~~~
第 282 行是 $.post(...
,第 24 列似乎是第一个“,”字符。最初我在这一行上有 JSON,但我将其向上移动(到 var myJSONObject ...
行),因为我认为与无效 JSON 相关的错误(行开头的选项卡给出了误导性) 号)。
var myJSONObject = {"body": "", "action": "postcomment", "submitted": "true", "ajax": "true"};
myJSONObject.body = $("body").val();
$.post("$!{articleurl}", myJSONObject, function(result){
btn.textContent='Comment sent successfully.';
});
列 更新
我更改了以下几行:
var url = "$articleurl";
$.post(url, myJSONObject, function(result){
~~~snip~~~
解析异常仍然集中在第一个“,”上,我假设问题是 Velocity 认为它应该能够解析 $.post -。事实上,我在其他 Velocity VM 模板中使用了 jQuery,没有任何问题。有没有办法让 Velocity 在解析时忽略某些行/
语句
? href="http://velocity.apache.org/engine/devel/user-guide.html#escapingvalidvtlreferences" rel="noreferrer">此链接关于在 Velocity 中转义引用,但它不能解决我的问题。在 $.post
之前添加“\”会给我完全相同的错误,但该列是一个额外的,因为在行开头添加了字符。
I'm trying to add a jQuery post to some JavaScript on a web page. The entire page is built up of several Velocity templates. Everything has been fine until I've tried to add the jQuery post, now I get:
org.apache.velocity.exception.ParseErrorException: Encountered "," at line 282, column 24 of /WEB-INF/velocity/www/comments.vm
Was expecting one of:
"(" ...
<RPAREN> ...
<ESCAPE_DIRECTIVE> ...
~~~snip~~~
Line 282 is $.post(...
and column 24 appears to be the first "," character. Initially I had the JSON on this line, but I moved it up (to the var myJSONObject ...
line)as I thought the error related to invalid JSON (tabs at the start of the line gave a misleading column number).
var myJSONObject = {"body": "", "action": "postcomment", "submitted": "true", "ajax": "true"};
myJSONObject.body = $("body").val();
$.post("$!{articleurl}", myJSONObject, function(result){
btn.textContent='Comment sent successfully.';
});
Minor Update
I changed the following lines:
var url = "$articleurl";
$.post(url, myJSONObject, function(result){
~~~snip~~~
The parse exception still focuses on the first ",". I'm assuming the issue is that Velocity thinks it should be able to resolve $.post - when in fact, it's jQuery. I've used jQuery in other Velocity VM templates without any problem. Is there a way to get Velocity to ignore certain lines / statements when parsing?
Update 2
I found this link about escaping references in Velocity, but it does not resolve my issue. Adding a "\" before $.post
gives me the exact same error, but the column is one extra, because of the character added at the start of the line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
#[[ ... ]]#
包装您的 javascript,它告诉 Velocity 不要解析包含的块(Velocity 1.7 中的新增功能)You can wrap your javascript with
#[[ ... ]]#
which tells Velocity to not parse the enclosed block (new in Velocity 1.7)好吧,似乎有两种解决方案:
首先,使用 jQuery,我们可以避免使用全局别名 $,而是直接使用 jQuery 对象:
在我的例子中,上面的方法效果很好。但我怀疑在其他场景(非 jQuery)中这可能是不可能的。在这种情况下,我们可以在有效的 Velocity 引用中“隐藏”我们的角色,如下所示:
来源:http ://velocity.apache.org/engine/devel/user-guide.html#escapinginvalidvtlreferences
我仍然想知道为什么反斜杠转义不起作用,但上面的将至少让我重新动起来。 :)
Ok, there appears to be two solutions for this:
First, with jQuery we can just avoid using the global alias $ and instead use the jQuery object directly:
In my case, the above works great. But I suspect in other scenarios (non-jQuery) this may not be possible. In which case, we can 'hide' our character within a valid Velocity reference like this:
Source: http://velocity.apache.org/engine/devel/user-guide.html#escapinginvalidvtlreferences
I'd still like to know why the backslash escape didn't work, but the above will at least get me moving again. :)
我认为这是 1.6.x 版本中的一个错误,因为它在 1.7 中工作正常(如果没有,请告诉我,我测试了很多次..),根据参考文献,
$
仅当其后跟a-zA-Z
时才生效。我想尝试调试到底发生了什么,但是翻译代码是由Java CC工具生成的,很难识别逻辑......I think this is a bug in version 1.6.x, because it works fine in 1.7(If it did not, please tell me, I test it many times..), according to the reference, the
$
takes effect only when it is followed bya-zA-Z
. I want to try do debug what happened really, but the translation code is generated by Java CC tool, it is too hard to recognize the logic...您必须使用 javascript 代码创建一个 js 文件
并将你的js文件导入到你的vm代码中
you must create a js file with your javascript code
and import your js file into your vm code
不幸的是,我无法让它与任何其他修复一起使用,例如在速度中转义“$”。我通过使用 jQuery 加载外部 js 文件而不是直接以速度编写 jQuery 来使其工作。至少对我有用,希望它能帮助别人:)
/björn
I couldn't get it to work with any of the other fixes like escaping "$" in velocity unfortunately. I got it working by loading an external js-file with the jQuery instead of writing jQuery directly in velocity. Worked out for me at least, hope it helps someone :)
/björn