速度模板和javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3879959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
velocity template and javascript
提问by Adi Sembiring
I try add javascript to my velocity template.
我尝试将 javascript 添加到我的速度模板。
<html>
<head>
<title>:: $currency.CurrencyName Detail Info ::</title>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>$currency.CurrencyName</td>
</tr>
<tr>
<td>Jual</td>
<td><div id="$currency.CurrencyName_buy">$currency.Buy</div></td>
</tr>
<tr>
<td>Beli</td>
<td><div id="$currency.CurrencyName_sell">$currency.Sell</div></td>
</tr>
</table>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
setInterval(function() {
$.get('updateCurrency.htm', function(data) {
$('#time').text(data);
});
}, 5 * 60 * 1000); // 1000 milliseconds = 1 second.
});
</script>
<p>Current date/time is: <span id="time"></span>
</body>
</html>
but I got the following error:
但我收到以下错误:
org.apache.velocity.exception.ParseErrorException: Encountered "," at line 28, column 29 of currencyDetail.html
Was expecting one of:
<EOF>
"(" ...
<RPAREN> ...
<ESCAPE_DIRECTIVE> ...
<SET_DIRECTIVE> ...
"##" ...
"\\" ...
"\" ...
<TEXT> ...
"*#" ...
"*#" ...
<STRING_LITERAL> ...
<IF_DIRECTIVE> ...
<STOP_DIRECTIVE> ...
<INTEGER_LITERAL> ...
<FLOATING_POINT_LITERAL> ...
<WORD> ...
<BRACKETED_WORD> ...
<IDENTIFIER> ...
<DOT> ...
"{" ...
"}" ...
at org.apache.velocity.Template.process(Template.java:137)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:415)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:335)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1102)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1077)
at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:528)
at org.springframework.web.servlet.view.velocity.VelocityView.getTemplate(VelocityView.java:535)
at org.springframework.web.servlet.view.velocity.VelocityView.getTemplate(VelocityView.java:520)
at org.springframework.web.servlet.view.velocity.VelocityView.checkTemplate(VelocityView.java:293)
at org.springframework.web.servlet.view.velocity.VelocityView.initApplicationContext(VelocityView.java:258)
at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:70)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:323)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1355)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:314)
at org.springframework.web.servlet.view.UrlBasedViewResolver.loadView(UrlBasedViewResolver.java:413)
at org.springframework.web.servlet.view.AbstractCachingViewResolver.createView(AbstractCachingViewResolver.java:159)
at org.springframework.web.servlet.view.UrlBasedViewResolver.createView(UrlBasedViewResolver.java:378)
at org.springframework.web.servlet.view.AbstractCachingViewResolver.resolveViewName(AbstractCachingViewResolver.java:78)
at org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1190)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1139)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:808)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
If I remove the javascript script, the web page loaded succesfully. any one can explain why ? I think javascript doesn't work if using velocity template.
如果我删除 javascript 脚本,网页加载成功。任何人都可以解释为什么?如果使用速度模板,我认为 javascript 不起作用。
采纳答案by serg
In Velocity 1.7b1 new syntax #[[this is included in output but not parsed]]#
was introduced:
在 Velocity 1.7b1#[[this is included in output but not parsed]]#
中引入了新语法:
#[[
$(document).ready(function() {
...
});
]]#
回答by Will Glass
When I mix jquery and Velocity, I find the simplest thing to do is to use "jQuery" instead of $, e.g.
当我混合使用 jquery 和 Velocity 时,我发现最简单的方法是使用“jQuery”而不是 $,例如
jQuery("#divid")
This avoids any difficulties -- escaping in velocity is tricky and non-intuitive.
这避免了任何困难——以速度逃逸是棘手且不直观的。
回答by Kees van Dieren
Another way is to add javscript to its own file, and use the #include vtl script directive.
另一种方法是将 javscript 添加到它自己的文件中,并使用 #include vtl 脚本指令。
E.g.:
例如:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
#include( "currencyDetail.js")
</script>
contents of currencyDetail.js (located in same dir / classpath package as .vm file):
currencyDetail.js 的内容(位于与 .vm 文件相同的目录/类路径包中):
$(document).ready(function() {
setInterval(function() {
$.get('updateCurrency.htm', function(data) {
$('#time').text(data);
});
}, 5 * 60 * 1000); // 1000 milliseconds = 1 second.
});
This was easiest for me.
这对我来说是最简单的。
回答by Underverse
If you do not have Velocity 1.7+ or need to use Velocity code as part of the javascript or jquery you can identify the key words in the javascript/jquery that are causing a problem and put them in a variable. Velocity will then render the output correctly.
如果您没有 Velocity 1.7+ 或需要使用 Velocity 代码作为 javascript 或 jquery 的一部分,您可以识别 javascript/jquery 中导致问题的关键字并将它们放入变量中。Velocity 然后将正确渲染输出。
First set the variable:
首先设置变量:
#set ( $replaceme = "each" )
Then use it in the code:
然后在代码中使用:
function reverseCommentOrder(){
$comments = AJS.$('#page-comments');
$comments.children().$replaceme(function(i,li){$comments.prepend(li)});
}
This results in javascript working on the page.
这会导致 javascript 在页面上工作。
回答by Underverse
Currently I am using Apache Velocity template.How Can I include JavaScript in Velocity template.I have tried all suggestion provide by Stack overflow forum.I did not get solution.Please suggest on this as soon as possible
目前我正在使用 Apache Velocity 模板。如何在 Velocity 模板中包含 JavaScript。我已经尝试了堆栈溢出论坛提供的所有建议。我没有得到解决方案。请尽快提出建议