When I'm using <%@ include %> directives in JSP, is there any way to have Eclipse syntax check my included files (or what i开发者_运维百科s 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?
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.
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
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: Variables in jsp pages with "included" pages
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.
精彩评论