Java variable across multiple include blocks - variable cannot be resolved

后端 未结 2 1480
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 16:04

I have jsp page that looks something like this:

MyPage.jsp

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


        
相关标签:
2条回答
  • 2020-12-28 16:44

    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.

    0 讨论(0)
  • 2020-12-28 16:48

    Best Practices

    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");
    %>
    

    Answer

    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.

    JSP validation off for fragments

    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:

    • files can be anywhere in your folder structure
    • the extension clearly indicates it's an include
    • new fragments will get identified automatically

    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.

    Validation 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.

    Exclude folder

    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?

    0 讨论(0)
提交回复
热议问题