Java variable in multiple include blocks - variable cannot be resolved

I have a jsp page that looks something like this:

Mypage.jsp

<%@ include file = "/Title.jsp" %> <%@ include file = "/Header.jsp" %> <html>...</html> 

There is an instance of "user" in Title.jsp, say:

 User user = new User(); 

in user Header.jsp is indicated as

 user.setName("John"); 

Eclipse shows an error that "the user cannot be resolved to a variable" in the header.jsp file. I understand why. Is there any way to tell Eclipse to ignore this particular error for this particular variable? Is there anything that can be done here?

At work, I support some old (and big and ugly) Java application, which is actually a web application. I am trying to convert this Java project to a Java Dynamic Web Project. Before I converted it, somehow Eclipse didn't care about these dangling variables that are everywhere. Now in a dynamic web project, Eclipse complains.

Rewriting the code is out of the question. Each jsp file consists of several inclusions with many global variables. I will not touch him more than I need.

+18
java variables eclipse include jsp
May 16 '13 at 17:37
source share
2 answers

Best practics

Ideally, the JSP should no longer have <% // scriptlet %> blocks.

JSPs have evolved so much that they simply hid Java code behind Standard Actions and custom tags using the expression language, JSTL, and now OGNL expressions to obtain results (for the processed request only) from pre-populated JavaBeans available in any application area (for example, a session , request, page, etc.) or complex data stores (e.g. ValueStack in Struts 2).

So, the correct solution will look something like this (cannot use EL, because we need the "user" link)

  <jsp:useBean id="user" class="foo.User" /> <!-- scope="page" by default --> 

This line of code, when added to Title.jsp will create a new User bean and add it to the page area, and the same line of code in Header.jsp will extract the User bean from the page and assign it a link called User , which can then be used throughout the rest of the file.

But, since the rest of the Java code is not written using tags, this will not make much sense. That way, you can also just 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"); %> 

Answer

If the code base is too large to save, and using any of the best practices is simply impractical; you can set up Eclipse to ignore JSP syntax checking errors, or it is better to disable them only for JSP fragments or specific files and folders.

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

JSP validation off for fragments

A JSP snippet is a .jspf file that contains a JSP / HTML segment without opening or closing header tags (unless, of course, it is a header or footer fragment). Therefore, although you will have to rename your files in .jspf for Eclipse in order to recognize them as fragments (and not check); Main advantages:

  • files can be located in any structure folder
  • extension explicitly indicates inclusion
  • new fragments are automatically identified

If the number of included files is huge or cannot be renamed for some reason, your next best option is to move them to a separate folder (for example, includes ), and then exclude the folder itself from JSP syntax checking.

Again, go to the menu "Project"> "Properties"> "Validation" and turn on the special settings of the project, click the "Search Type" [...] button to access the JSP Syntax Validator settings.

Validation settings

In this case, first create an Exclude Group and then Add Rule to exclude the folder (for example, WebContent / includes in the image) from the JSP syntax check.

Exclude folder

Eclipse will now stop reporting errors for any JSP file included in this folder. You could use the same approach for individual files , but how practical it will be again depends on the number of fragments that you have.

References:
How to avoid Java code in JSP files?

+28
May 24 '13 at 17:37
source share

To your question: is there a way to tell Eclipse to ignore this particular error for this particular variable? As noted above, the error comes from a validation framework in Eclipse. The best you can do is tell eclipse to ignore this file - not this error or this variable. To do this, right-click on the project and drag its properties dialog. After that, select "Verification" and check the box "Enable specific project settings." From there you can enable or disable special checks. Personally, I don’t find Eclipse validators all that is useful, and they take quite a lot of time to process, so I disabled almost all of them! However, you can control the fine grains through the "Settings" column. You get to elipsis next to the JSP Content Validator (for example), and you can exclude only Header.jsp from it.

Hope this helps, but only details the earlier comments.

+5
May 24 '13 at
source share



All Articles