I have jsp page that looks something like this:
<%@ include file = "/Title.jsp" %> <%@ include file = "/Header.jsp" %> <html>...</html>
In Title.jsp there is a instance of "user", let's say:
User user = new User();
in Header.jsp user is referenced as
user.setName("John");
Eclipse is showing error that "user cannot be resolved to variable" in header.jsp file. I perfectly understand why. Is there a way to tell Eclipse to ignore this particular error for this particular variable? Is there anything that can be done here?.
At work, I am supporting some old (and big and ugly) Java application that is actually a web application. I am trying to convert this Java Project to Java Dynamic Web Project. Before I converted it, somehow Eclipse didn't care about these hanging variables that are all over the place. Now, in Dynamic Web Project, Eclipse is complaining.
Rewriting the code is out of question. Each jsp file is consisted of multiple includes with tons of global variables. I don't won't to touch it more than I need to.
To fix the Java “cannot be resolved to a variable” error, you must declare the variable inside the scope where you will utilize it. The missing variable declaration is the most common cause of this error. That's why it is important to declare a variable before using it in the current scope.
Fix the cannot be resolved to a variable Error in Java If you try, the cannot be resolved to a variable error will come out. It means it cannot detect the initialization of variables within its scope. Similarly, if you make a private variable, you cannot call it inside a constructor. Its scope is out of bounds.
The syntax for an initializer is the type, followed by the variable name, followed by an equal sign, followed by an expression. That expression can be anything, provided it has the same type as the variable. In this case, the expression is 10, which is an int literal.
Ideally, one shouldn't have any <% // scriptlet %>
blocks in JSPs any more.
JSPs have evolved so much from simply hiding Java code behind Standard Actions and Custom Tags to using Expression Language, JSTL and now OGNL expressions to fetch the results (for the request just processed) from pre-populated JavaBeans available in any one of the application scopes (like session, request, page etc.) or sophisticated data stores (like ValueStack in Struts 2).
So, a proper solution would look something like (can't use EL because we need a "user" reference)
<jsp:useBean id="user" class="foo.User" /> <!-- scope="page" by default -->
This line of code when added to Title.jsp
would create a new User
bean and add it to the page
scope and this same line of code in Header.jsp
would then retrieve the User
bean from the page
scope and give it a reference named user
that can then be used throughout the rest of the file.
But, since the rest of the Java code isn't written with tags this won't make much sense. So, you could also simply pass the bean between the two JSPs as a request attribute using one of your <% // scriptlet %> blocks.
<% // in Title.jsp request.setAttribute ("user", new User()); %> <% // in Header.jsp User user = request.getAttribute ("user"); user.setName ("John Doe"); %>
If the code base is too huge to salvage and using any of the best practices is just plain impractical; you could configure your Eclipse IDE to ignore JSP syntax validation errors or better only turn them off for JSP fragments or specific files and folders.
With your web project selected in the workspace go to Project > Properties > Validation > JSP Syntax
. Then Enable project specific settings
and turn off Validate JSP fragments
as shown below.
A JSP fragment is a .jspf
file that contains a JSP/HTML segment without opening and closing header tags (unless it's a header or a footer fragment of course). So, although you would have to rename your files to .jspf
for Eclipse to recognize them as fragments (and not validate); the main advantages are:
If the number of include files is huge or they can't be renamed for some reason your next best option would be to move them into a separate folder (like includes
) and then exclude the folder itself from JSP syntax validation.
Again, go to Project > Properties > Validation and with project specific settings enabled click on the browse type [...]
button for accessing the JSP Syntax Validator
settings.
In here, first create an Exclude Group
and then Add Rule
to exclude a folder (like WebContent/includes in the image) from JSP syntax validation.
Now Eclipse would stop reporting errors for any JSP file dropped in this includes folder. You could use the same approach for individual files as well but how practical it is would again depend on how many such fragments you have.
References:
How to avoid Java Code in JSP-Files?
To your question: Is there a way to tell Eclipse to ignore this particular error for this particular variable? As noted above, the error is coming from the validation framework in Eclipse. The best you can do is to tell eclipse to ignore that file - not that error or that variable. To do so, right click on the project and pull up it's properties dialog. Once there, select "Validation" and click the checkbox for "Enable project specific settings". From there you can turn on or off specific validations. Personally, I don't find the Eclipse validators all that useful and they take quite a bit of processing time, so I turn almost all of them off! However, you have some fine grain control via the "Settings" column. You hit the elipsis next to JSP Content Validator (for example) and you can specifically exclude only Header.jsp from there.
Hope this helps, it only provides the details to the earlier comments though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With