4

I have a Spring 4.0.9 MVC application using JSPs as the view technology and Maven 3.2.5 for the build management. And I use Eclipse 4.4.1 for development and Tomcat 7.0.57 for running the application.

In the JSPs I want to use the EL expression ${pageContext.request.contextPath} to reference the current request path. But Eclipse gives me the error:

javax.servlet.jsp.JspException cannot be resolved to a type

I added the following Maven dependency:

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

and the error disappears, but now I get some new types of warnings. Wherever I use the EL expression above, Eclipse says:

The declared exception IOException is not actually thrown by the method _elExpression7505() from type __2F_myproject_2F_src_2F_main_2F_webapp_2F_WEB_2D_INF_2F_views_2F_page_2E_jsp

And everywhere I use Spring's form:label tag it tells me:

Unnecessary cast from Object to Object

Does anybody know how to get rid of these warnings? Do I have to use other Maven dependencies? (I've also included javax.servlet-api 3.0.1, jstl 1.2 and many more)

Edit: I do not want to globally ignore this type of warnings for all Java files. To me, this would rather be a workaround than a real solution.

Sebastian S.
  • 1,545
  • 6
  • 24
  • 41
  • I generally just ignore the random warnings Eclipse shows you. It works fine at run time ;) – ConMan Jun 12 '15 at 13:36
  • Yes, on runtime there are no problems. It's just ugly having these warnings in Eclipse. – Sebastian S. Jun 15 '15 at 09:56
  • are these warnings just redundant or are they actually _wrong_? If wrong, then it would be an Eclipse issue. I get the same errors, and I just tried pulling out the generated JSP java classes from the server which is presumably where the warning is generated - workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\ETL4ECN\org\apache\jsp\WEB_002dINF\jsp - but I had no luck pinning it down. – Adam Sep 07 '15 at 14:39

2 Answers2

2

Luckily, Eclipse has a place to edit warning/error preferences:

Project Properties -> Java Compiler -> Errors/Warnings

There's a nice search box which you can use to track down the error/warning setting you'd like to edit (for example you could look for "Unnecessary cast"). In the dropdown to the right of the entry that shows up, you can then select Ignore. Hit "Apply", rebuild the project and you should have gotten rid of the warnings you wanted gone.

You would have to set the following warnings to "ignore":

  • Unnecessary cast or 'instanceof' operation
  • Unnecessary declaration of thrown exception

See this help page for reference.

Edit: According to this answer on Stackoverflow, you can include folders from being checked for warnings. I haven't tested this myself, so this is a long shot.

Community
  • 1
  • 1
JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
  • This solution would imply, that these kind of warnings are also ignored for all Java files. And since I have to follow some very restrictive company-wide guidlines, this is not the best option for me. – Sebastian S. Jun 22 '15 at 14:35
  • @SebastianS. Do you have the jars in a folder? – JackWhiteIII Jun 22 '15 at 14:36
  • I wonder why there isn't a fix yet, since JSPs are not a bleeding edge technology and I would assume many people must have seen this warnings. – Sebastian S. Jun 22 '15 at 14:37
  • I have the typical tomcat jars in its lib directory. – Sebastian S. Jun 22 '15 at 14:41
  • @SebastianS. Does that directory belong to the Eclipse project, so that it is in the project build path? Can you not access the directory using Eclipse? – JackWhiteIII Jun 22 '15 at 14:43
  • No, it does not belong to the project and is not on the build path. Only the Maven dependencies (scope: provided) are. To my understanding this would be enough. – Sebastian S. Jun 22 '15 at 14:48
  • I unfortunately don't know a solution to that particular problem, but I'm going to edit my answer and add a general solution that I've found digging on Stackoverflow. I'd suggest you to edit your question and mention that you need to keep your .java files intact. – JackWhiteIII Jun 22 '15 at 14:51
2

At the top of your JSP, you can include the following:

<%! @SuppressWarnings("cast") %>

This will prevent eclipse from showing the eclipse "unnecessary cast object to object" warning.

Nayan Hajratwala
  • 350
  • 1
  • 4
  • 11