javaeclipsejspjsp-fragments

SuppressWarnings in jsp file


How do you suppress warnings (or even errors) in a jsp fragment file in Eclipse?

I have this jspf that I include in the middle of the jsp file, but that jspf relies on a variable further up in the code before it is included. However, even though all works fine, every time I validate my codes (before deployment), Eclipse marks that jspf with an error. I can safely ignore the error and proceed with the deployment, but is there a way to remove that error through a suppress warning annotation in that jspf file?

main.jsp:

<% 
  String someVariable = "foo.bar"; 
%>
<% @ include file="WEB-INF/jspf/sample.jspf" %>

WEB-INF/jspf/sample.jspf

<%
  // shows unresolved local variable error when validated
  // but can still deploy just fine if error is ignored
  if(someVariable != null && someVariable.length() > 0)
    out.println(someVariable); 
%>

The example above is very simple, but sample.jspf will never be called directly from URL. It will always be included in some other jsp file. Now how to remove error mark in Eclipse when validating?


Solution

  • How sample.jspf knows that it will become a part of main.jsp?

    JSP is finally converted to Scriplet .java file as shown below and it's something like you are using a local variable that is not declared anywhere.

    sample_jsp.java

    public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {
         JspWriter out = null;
         ...
         out.write(someVariable); // local variable that is never declared
         ...
    }
    

    Alternate solution

    If you don't want to use JSTL then try with Scriplet but never suggest you to use it at all. First set it as the request or session attribute in first JSP and access it in another JSP.

    main.jsp:

    <% 
         String someVariable = "foo.bar"; 
         request.setAttribute("someVariable",someVariable);
    %>
    
    <% @ include file="WEB-INF/jspf/sample.jspf" %>
    

    sample.jspf

    <%  
        out.println(request.getAttribute("someVariable")); 
    %>
    

    Try to avoid scriplet instead use JSP Standard Tag Library and Expression Language.

    main.jsp:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <c:set var="someVariable" value="foo.bar"/>
    

    sample.jspf

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <c:if test="${not empty someVariable}">
        <c:out value="${someVariable}"/>
    <c:if>