0

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?

kerafill
  • 274
  • 1
  • 4
  • 14
  • If it is never called why not comment the unresolved parts – SparkOn Jun 25 '14 at 10:16
  • The sample.jspf file will never be called directly from URL. It will be placed in a secure directory where only includes are allowed. I'll update the example to reflect some conditions. – kerafill Jun 25 '14 at 11:32

2 Answers2

0

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>
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I'm sorry but I think you didn't understand my question correctly. I only want to suppress compiler errors in a jsp file, not solve any errors whatsoever. – kerafill Jun 25 '14 at 10:02
  • I get your point about using JSP Tag Library. The example above only contains one line in the included jspf file. What if I had to process the someVariable and it requires more lines, using tag library will have lots of tags to do this? – kerafill Jun 25 '14 at 11:30
  • updated it in my post. `not empty` EL checks for not null as well as empty string as well. – Braj Jun 25 '14 at 11:43
  • So basically, I have to convert all the java (10 lines or more) in the jspf file into tags? – kerafill Jun 25 '14 at 11:47
  • Yes you can convert it easily. Is there any issue? If you are not able to do then share the code or read more about JSTL first. – Braj Jun 25 '14 at 12:03
  • I'm new to JSP and I don't know much about the tag library. I'm more comfortable to continue writing java code. Aside from converting to JSTL, is there no suppress warning annotation that would work for the above error? – kerafill Jun 25 '14 at 12:08
  • You are coding in 21st century. Read more [Code Conventions for the JavaServer Pages Technology Version 1.x Language](http://www.oracle.com/technetwork/articles/javase/code-convention-138726.html) what it states :Where possible, avoid JSP scriptlets whenever tag libraries provide equivalent functionality. This makes pages easier to read and maintain, helps to separate business logic from presentation logic, and will make your pages easier to evolve into JSP 2.0-style pages (JSP 2.0 Specification supports but deemphasizes the use of scriptlets). – Braj Jun 25 '14 at 12:37
  • Do you know how to use html tags, if yes then it's just like html tags. Just read the basic of JSTL and make a better application. – Braj Jun 25 '14 at 12:38
  • Making the page easier to read and maintain is very subjective. Scriptlets provide business logic, too. But I'm not here to argue that. And yes, I know how to use HTML tags. I'd appreciate it if you kept your condescension to yourself. Making a better application is also subjective. I simply asked a question of how to suppress warnings/errors in a jsp file. If there's no direct answer, just say there is none. I will look into JSTL as you have suggested. But if it's too tedious, I'll just keep ignoring the errors/warnings. The application still works on deployment anyway. – kerafill Jun 25 '14 at 12:54
  • I haven't added any thing in my comment from my own, I just copied it directly from Oracle official site. By the way nice response. Find the answer in the first line of my post. – Braj Jun 25 '14 at 12:56
  • 1
    try `<%! @SuppressWarnings("unchecked") %>` and read more [HERE](http://stackoverflow.com/questions/487715/is-there-an-equivalent-to-java-suppresswarnings-in-jsp) and [HERE](http://stackoverflow.com/questions/4925708/suppressing-java-unchecked-warnings-in-jsp-files) and [HERE](http://www.coderanch.com/t/462333/JSP/java/fix-unchecked-warnings-JSP-pages). but I don't think that this will resolve it in this case because JSP file in converted to Scriplet .java file and if you use a local variable without declaring it then what happens, you know it very well. – Braj Jun 25 '14 at 12:59
  • I have edited in in my post that will solve your issue using Scriplet. – Braj Jun 25 '14 at 13:25
  • I've been studying JSTL the past few days, and I'll go with your suggestion. I have some questions about JSTL but I'll post it in another topic. I don't use built-in sessions in JSP, so my questions lie on whether variables declared automatically creates a JSP session. I'll just read up more on the documentation. – kerafill Jul 02 '14 at 05:53
0

You may use it in a Scriptlet <% @SuppressWarnings("unchecked") %>, see this post.

Community
  • 1
  • 1
Downhillski
  • 2,555
  • 2
  • 27
  • 39