tags:

views:

179

answers:

4

When I'm using <%@ include %> directives in JSP, is there any way to have Eclipse syntax check my included files (or what is best practice for this). What I mean is that if I include files that have variables declared in the parent, I get a bunch of errors about undeclared variables (makes sense).

So how do people get around this?

A: 

Hi Mason,

Are you sure you are using

<%@ include ... %>

and not

<jsp:include ... >? 

I would like to clarify that this is the scenario you are talking about:

includeMe.jsp:

<%@ page language="java" %>
<%
boolean testBoolean = true;
%>

App.jsp:

<%@ page language="java" %>
<%@include file="/WEB-INF/includeMe.jsp"%>
<%
testBoolean = false;
%>

And you are getting the undeclared variable error on testBoolean in App.jsp file? If this is the case, I'm not sure what is wrong, because I can't reproduce this issue in Eclipse 3.5 (with WTP installed).

This might help as well: http://stackoverflow.com/questions/943770/variables-in-jsp-pages-with-included-pages

kg
I am doing the opposite, declaring a variable in App.jsp and accessing the variable in includeMe.jsp. I will just move it all to Java classes and tags.
Mason
+2  A: 

Disable JSP validation in Eclipse (it has always been a failure), or, better, just don't use scriptlets. It's considered bad practice. Keep Java code in real Java classes and use taglibs/EL in JSP all the way.

BalusC
+2  A: 

The best practice is to encapsulate all Java code as tags, and then use the tags in your JSP. As a bonus when you do this you'll be writing your java in .java files, whose syntax Eclipse will check for you. You can find more information about tags from Sun

justkt
A: 

The contents of your fragment will be validated as part of validating the main file, meaning any errors found will be reported back to the include directive in the main file. Your fragment may be correct or invalid for different reasons depending on which file is including it, assuming any files include it at all.

nitind