Java 神秘的 Eclipse JSP 验证错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3789886/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:00:10  来源:igfitidea点击:

Mysterious Eclipse JSP Validation Errors

javaeclipsevalidationjsp

提问by worpet

Eclipse (Helios) occasionally marks valid looking JSP content as having errors. It seems like it often breaks when I use the <c:if> tag. For example, in a JSP with just this content:

Eclipse (Helios) 有时会将看起来有效的 JSP 内容标记为有错误。当我使用 <c:if> 标签时,它似乎经常中断。例如,在一个只有这个内容的 JSP 中:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>

<c:if test="${1 == 1}">
something
</c:if>

</body>
</html>

The following errors show in the "Problems" tab after I compile:

编译后,“问题”选项卡中显示以下错误:

  • Incompatible operand types String and int line 1
  • javax.servlet.jsp.JspException cannot be resolved to a type line 1
  • javax.servlet.jsp.PageContext cannot be resolved to a type line 1
  • 不兼容的操作数类型 String 和 int 第 1 行
  • javax.servlet.jsp.JspException 无法解析为类型第 1 行
  • javax.servlet.jsp.PageContext 无法解析为类型行 1

The code runs fine. Does the validation for JSPs have issues, am I missing something obvious, or does this indicate something isn't set up correctly.

代码运行良好。JSP 的验证是否有问题,我是否遗漏了一些明显的东西,或者这是否表明某些设置不正确。

采纳答案by worpet

Based on the comments, I ended up turning off part of the JSP validation, which fixed this.

根据评论,我最终关闭了部分 JSP 验证,从而解决了这个问题。

  1. Go to "Project->Properties->Validation".
  2. Click "Configure Workspace Settings...".
  3. Unselect options for JSP Syntax Validator (both manual and build).
  1. 转到“项目->属性->验证”。
  2. 单击“配置工作区设置...”。
  3. 取消选择 JSP 语法验证器的选项(手动和构建)。

I was hoping I was missing something and there was a way to fix this, but I have to concede that the JSP validation is junk.

我希望我遗漏了一些东西,并且有办法解决这个问题,但我不得不承认 JSP 验证是垃圾。

回答by Vivien Barousse

Try to check your project classpath. It looks like you don't have the JSP library in your project (hence the "JspException cannot be resolved"), or that your library version isn't the same as the JSP compiler version.

尝试检查您的项目类路径。看起来您的项目中没有 JSP 库(因此“无法解析 JspException”),或者您的库版本与 JSP 编译器版本不同。

The library is included by default in the application server on which you deploy your app, so the code runs perfectly when deployed. However, if the Eclipse internal compiler is missing a library (or have an incorrect version), the Eclipse editor shows you error that doesn't exists in app server.

该库默认包含在您部署应用程序的应用程序服务器中,因此代码在部署时可以完美运行。但是,如果 Eclipse 内部编译器缺少库(或版本不正确),Eclipse 编辑器会向您显示应用服务器中不存在的错误。

回答by Paranoja

In order to fix:
- javax.servlet.jsp.* needs jsp-api.jar
- javax.servlet.http.* needs servlet-api.jar

You need to add these libraries to Java Build path in project setup. These libraries can be found in tomcat/lib directory (for Tomcat 6.0).

为了修复:
- javax.servlet.jsp.* 需要 jsp-api.jar
- javax.servlet.http.* 需要 servlet-api.jar

您需要将这些库添加到项目设置中的 Java Build 路径中。这些库可以在 tomcat/lib 目录中找到(对于 Tomcat 6.0)。

回答by Christiaan

I had the same problem and eventually got it to work by switching (with Maven) to version 2.3 of the servlet-api:

我遇到了同样的问题,并最终通过(使用 Maven)切换到 servlet-api 的 2.3 版使其工作:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.3</version>
    <scope>provided</scope>
</dependency>

Apparently 2.4 and higher don't have javax.servlet.jsp build in anymore. You might be able to find that library in another package if you still want to use 2.4 or higher.

显然 2.4 及更高版本不再内置 javax.servlet.jsp。如果您仍想使用 2.4 或更高版本,则可以在另一个包中找到该库。

回答by Mark Joseph Del Rosario

Well, I found how to solve this error. Add this to your Maven dependency(pom.xml):

好吧,我找到了解决此错误的方法。将此添加到您的 Maven 依赖项(pom.xml):

<!-- dependency to fix JSPServletException -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>jsp-api</artifactId>
        <version>6.0.32</version>
        <scope>provided</scope>               
    </dependency>

Do comment if you find it useful, as much as it helped me.

如果您觉得它有用,请发表评论,因为它对我有帮助。

回答by kmajetic

Solution for eclipse dependencies which should not be deployed is adding UserLibrary container, and include it in dependent projects.

不应该部署的eclipse依赖的解决方案是添加UserLibrary容器,并将其包含在依赖项目中。

  1. Create dir e.g /home/user*/eclipse-deps/ and copy jsp-api.jar from */tomcat/lib dir
  2. In the Eclipse go to project properties->build path, and create new "UserLibrary" eg. IdeDeps.
  3. Include /home/user/eclipse-deps/ in IdeDeps library, and include user library in all projects which are dependent on jsp-api.jar (in classpath: < classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/IdeDeps"/ >)
  1. 创建目录例如 /home/user*/eclipse-deps/ 并从 */tomcat/lib 目录复制 jsp-api.jar
  2. 在 Eclipse 中,转到项目属性-> 构建路径,然后创建新的“UserLibrary”,例如。德普。
  3. 在 IdeDeps 库中包含 /home/user/eclipse-deps/,并在所有依赖于 jsp-api.jar 的项目中包含用户库(在类路径中:< classpathentry kind="con" path="org.eclipse.jdt. USER_LIBRARY/IdeDeps"/ >)

You can do that for all *.jars which should not be deployed, but eclipse need it for validation purpose.

您可以为所有不应部署的 *.jars 执行此操作,但 eclipse 需要它进行验证。

回答by Vishal

You need to include jap-api.jar in your classpath.

您需要在类路径中包含 jap-api.jar。

回答by Hymanxie

Adding jsp-api.jarinto your build classpath would fix it. jsp-api.jaris under common\libfor tomcat 5.x

添加jsp-api.jar到您的构建类路径中将修复它。jsp-api.jarcommon\libtomcat 5.x 下

回答by deng hui

I also see the same problem during I shift to Maven (with m2e), get error like "javax.servlet.http cannot be resolved" and some Spring tags undefined warning. finally I find it's because java version (v1.6) in my build configuration is not same as in facets configuration (v1.7). after change v1.7 to v1.6 the problem disappear.

在我转移到 Maven(使用 m2e)时,我也看到了同样的问题,得到类似“javax.servlet.http 无法解析”的错误和一些 Spring 标签未定义警告。最后我发现这是因为我的构建配置中的 java 版本 (v1.6) 与 facets 配置 (v1.7) 中的不同。将 v1.7 更改为 v1.6 后,问题消失。

回答by rjcorujo

I had the same problem, the problem is the jsp-apilibrary, you can add the dependency to your pom(as explained in other answers) or you can also add the targetrun-time and eclipse will automatically add that library to your class-path:

我遇到了同样的问题,问题是jsp-api库,您可以将依赖项添加到您的pom(如其他答案中所述),或者您也可以添加目标运行时,eclipse 会自动将该库添加到您的类路径中:

Project -> Properties -> Targeted Runtimes 

And select your server.

并选择您的服务器。