简体   繁体   English

为什么Eclipse为什么抱怨我的JavaScript中缺少分号?

[英]Why does Eclipse complain about missing semicolon in my javascript?

I'm having an issue with Eclipse where it's complaining about missing semicolons in javascript code in a jsp file. 我在Eclipse中遇到问题,它抱怨jsp文件中javascript代码中缺少分号。 Given the two lines below, Eclipse complains about the first line and indicates that there's a missing semicolon right before the closing curly brace. 给定下面两行,Eclipse抱怨第一行,并指出在右花括号前缺少分号。 It has no complaints about the second line. 它没有对第二行的抱怨。 I'd prefer to use the first way, but I'm annoyed with the warnings. 我更喜欢使用第一种方法,但是我对警告感到恼火。 Is there a different syntax that I should be using? 我应该使用其他语法吗? I'm also using JQuery, so I don't know if this is contributing to the parsing error in Eclipse. 我也在使用JQuery,所以我不知道这是否导致Eclipse中的解析错误。

var isFoo = ${actionBean.isFoo};  // javascript type is boolean

var isFoo = '${actionBean.isFoo}';  // javascript type is string

To answer some of the questions people have posed in the comments... 为了回答人们在评论中提出的一些问题...

This code is from a JSP file and actionBean refers to the Java action bean for this page. 这段代码来自JSP文件,actionBean引用此页面的Java action bean。 "isFoo" is a member variable of the action bean. “ isFoo”是操作bean的成员变量。 The syntax ${actionBean.isFoo} is JSP Expression Language (or EL for short) and it's used to evaluate a java variable in a JSP. 语法$ {actionBean.isFoo}是JSP表达式语言(简称EL),用于评估JSP中的Java变量。 In this case, my code takes the value from the java variable and assigns it to the javascript variable. 在这种情况下,我的代码从java变量中获取值并将其分配给javascript变量。 The code works just fine, but Eclipse complains. 该代码可以正常工作,但是Eclipse抱怨。

var isFoo = ${actionBean.isFoo};

This is not valid JavaScript, which is why you're getting a syntax error. 这是无效的JavaScript,这就是为什么您遇到语法错误的原因。 I'm assuming the ${...} is supposed to do some interpolation in NetBeans, but Eclipse has no way of knowing that, and just tries to parse it as JavaScript. 我假设${...}应该在NetBeans中进行一些插值,但是Eclipse没有办法知道,只是尝试将其解析为JavaScript。 Unless there's an Eclipse plugin or setting for dealing with mixed code like this, you may just have to deal with seeing syntax errors. 除非有一个Eclipse插件或用于处理这种混合代码的设置,否则您可能只需要处理语法错误即可。

Eclipse is complaining because it is not really Javascript, and it is trying to parse it like Javascript. Eclipse之所以抱怨是因为它不是真正的Javascript,而是试图像Javascript一样解析它。 If it is really annoying you could disable the warning in the preferences, under your own risk to not be notified in other circumstances: 如果确实很烦人,则可以在首选项中禁用警告,如果在其他情况下不通知您,则自行承担风险:

禁用分号

var isFoo = ${actionBean.isFoo};

不是javascript,而是JSF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM