开发者

Mysterious Eclipse JSP Validation Errors

开发者 https://www.devze.com 2023-01-17 17:25 出处:网络
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) 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:

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

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

</body>
</h开发者_运维百科tml>

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

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.


Well, I found how to solve this error. Add this to your Maven dependency(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.


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

  1. Go to "Project->Properties->Validation".
  2. Click "Configure Workspace Settings...".
  3. Unselect options for JSP Syntax Validator (both manual and build).

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.


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

Project -> Properties -> Targeted Runtimes 

And select your server.


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).


This is an old question but thought I might mention I get this too in Juno on Mac OS X—specifically most often after i change a file externally and then refresh the project in Eclipse. Underlines in all sorts of odd places, even halfway through words in JSP comments.

Could (likely?!) be related to bug 376926 which apparently got fixed a bit over a week ago?


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

  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"/ >)

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


For Tomcat 7.0.x you need the following in your pom

<properties>
    <org.apache.tomcat.version>7.0.28</org.apache.tomcat.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jsp-api</artifactId>
        <version>${org.apache.tomcat.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>


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.

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.


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


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

<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.


I had the same problem, but both jsp-api.jar and servlet-api.jar were already in build path. Disabling JSP Syntax Validator didn't help as well.

Instead, I had to disable JSP Content Validator, leaving the syntax validator enabled. I still have underlined code in my JSP but no red cross indicating compiling error and that's the point :)


A much easier way is to

select the project -> Properties -> Java Build Path -> Libraries Tab

Select add Libraries. From there select Server Runtime. It will list all the server runtimes you have configured. From there Select the server runtime you have assosiated your project with.

This will rebuild and revalidate the projece and all this ghost errors will be removed.

Hope this helps.


For me, while I was typing the line manually, I got an error until I closed the tag, but the error never went away. I found a line of code that was similar, and I deleted the offending code, copy and pasted the the good code, and then made changes inside the tag as needed. No more errors!!! Not an ideal solution, but it worked for me.


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.


Adding jsp-api.jar into your build classpath would fix it. jsp-api.jar is under common\lib for tomcat 5.x


As many of you have mentioned, the issues is around the build path and missing libraries. I found a very simple fix to this build path issue. Here is the solution: Under 'Java Build Path' in eclipse, go to the 'Order and Export' tab, and make sure that the 'Apache Tomcat v7.0' libraries (or whichever version of Tomcat you installed with eclipse) are checked. This instantly fixes the problem. No hassle with anything else. :-)

There is no need to turn off any validation in that case either.

0

精彩评论

暂无评论...
验证码 换一张
取 消