JavaScript 中的 jstl
可以在javascript中使用jstl吗?
我正在设置
,然后在 html 中访问该值并执行一些代码。我的问题是即使 javascript 条件为 false,c:set 也始终执行。
<script type="text/javascript">
var v1 = false;
<c:set var"abc" value="yes"/>
if(v1){
<c:set var"abc" value="no"/>
}
</script>
在上面的代码中,即使 v1 为 false,'no' 也会设置为 abc。
Is it possible to use jstl inside javascript?
I'm tying to set <c:set var="abc" value="yes"/>
and then later access this value in html and execute some code. My problem is the c:set is executing always even if the javascript condition is false.
<script type="text/javascript">
var v1 = false;
<c:set var"abc" value="yes"/>
if(v1){
<c:set var"abc" value="no"/>
}
</script>
In the above code, even if v1 is false 'no' is setting to abc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您谈论服务器端 JSP 解释时,“在 Javascript 内部”的想法没有任何意义。就该领域而言,根本不存在 Javascript 这样的东西。
因此,是的,如果您的意思是您想做类似的事情,
请注意,您必须小心引用和特殊字符。当您将 JSP 变量插入到将被浏览器解释为 Javascript 源代码的页面部分时,JSTL
fn:escapeXml()
函数毫无用处。您可以使用 JSON 库对字符串进行编码,也可以编写自己的 EL 函数来执行此操作。There is no meaning to the idea of being "inside Javascript" when you're talking about server-side JSP interpretation. There's just no such thing as Javascript as far as that domain is concerned.
Thus, yes, it's possible, if you mean that you want to do something like
Note that you have to be careful with quoting and special characters. The JSTL
fn:escapeXml()
function is useless when you're interpolating a JSP variable into a part of the page that will be interpreted as Javascript source by the browser. You can either use a JSON library to encode your strings, or you can write your own EL function to do it.这是一个相当古老的线程,但这是我遇到同样问题时遇到的一个。由于我自己想到了一个解决方案,因此我会将其发布在这里,以防将来对某人有所帮助。
html (或 jsp )文件在声明为 javascript 源的外部文件中查找文本。
Tomcat(或类似的)仅解释具有 .jsp 扩展名的文件中的 JSTL 标记(或者也可能是其他一些标记,但与此答案无关)。
因此,重命名您的 .js 文件以赋予其 .jsp 扩展名(例如,将 javascript.js 更改为 javascript_js.jsp)
在 javascript_js.jsp: 的顶部添加这些行
,然后保留代码不变。
显然,如果在标头中使用除 c: 之外的其他前缀,则还需要添加更多前缀。
如果您使用 Eclipse(不知道其他 IDE),它会假设它不是 javascript 文件,并且您会丢失不同关键字(var、function 等)的配色方案、var 名称自动完成和自动缩进。
为了欺骗 IDE,您可以
在实际代码之前(在“<%@”声明之后)添加 js 注释,并
在文件末尾再次添加 js 注释。
这对我有用。
This is a quite old thread, but it is one I bumped into when I had the same problem. Since I thought of a solution myself, I will post it here in case it helps somebody in the future.
The html ( or jsp ) file looks for the text inside the external file declared as javascript source.
Tomcat ( or similar ) only interpret JSTL tags within files with the .jsp extension ( or maybe some other ones too, but it is irrelevant for this answer ).
So, rename your .js file to give it a .jsp extension ( javascript.js to javascript_js.jsp for example )
Add those lines at the top of javascript_js.jsp:
and just leave the code it unchanged.
Obviously, you also need to add more prefixes if you use some other than c: in the header.
If you use Eclipse ( don't know about other IDEs ), it will assume it is not a javascript file and you lose the colour scheme for the different keywords ( var, function and so on ), var name auto completion and auto indentation.
To fool the IDE, you can add
as a js comment, before the actual code ( after the "<%@" declarations ), and
at the end of the file, again as a js comment.
It worked for me.