How to debug JSTL?
Asked Answered
V

4

16

I'm using SpringSource Tool Suite (with Roo) and have some success. What bothers me though is that I don't know how to debug tag library-stuff.

I may add breakpoints but it never stops at them.

What I'm looking for is a dump of all current variables in the context.

Up until now I did something like:

<c:forEach items="${data}" var="item">
    <c:out value="${item}"></c:out><br />
</c:forEach>

Sadly, that's difficult to read and also not pretty straightforward.

What can I do to improve this?

Vermicelli answered 4/8, 2011 at 17:23 Comment(0)
C
6

Sorry, but you can't put debug points to a file containing markup. In this case, the tag library definition is in the form of a markup. So, instead of debug, you only get validation support for them.

An exception would be a Java Server Pages (JSP) file, which would be converted to a servlet (program code) at the runtime.

Debugging is only possible for scripts and code which have a defined execution sequence.

The best way to overcome this is to go through the documentation carefully and incrementally implementing the tags after getting knowledge on XML or the related markup language.

Cerium answered 5/8, 2011 at 13:31 Comment(0)
S
20

this was always tricky, for future visitors of this question adding my 2 cents

if you put a break point on the jsp in Eclipse on the line

Eclipse debug for JSTL

Superfluous answered 2/8, 2013 at 16:29 Comment(0)
C
6

Sorry, but you can't put debug points to a file containing markup. In this case, the tag library definition is in the form of a markup. So, instead of debug, you only get validation support for them.

An exception would be a Java Server Pages (JSP) file, which would be converted to a servlet (program code) at the runtime.

Debugging is only possible for scripts and code which have a defined execution sequence.

The best way to overcome this is to go through the documentation carefully and incrementally implementing the tags after getting knowledge on XML or the related markup language.

Cerium answered 5/8, 2011 at 13:31 Comment(0)
P
1

You can write a custom tag that accesses the spring context and dumps the current variables. You can also write a custom tag (or tags) that dump the contents of the Application, Session, Page, and Request scopes.

take a look at the Java EE 6 API. Look up the SimpleTag to start implementing a tag. Here is a link to the custom tag section in the Java EE 5 tutorial.

Example (dumping request scope):

  class MyTag extends TagSupport
  {
    public int doEndTag()
    {
      Enumeration attributeNames;
      Object attributeValue;
      String currentName;
      int nameIndex;
      Iterator nameIterator;
      JspWriter pageOut = pageContext.getOut();
      ServletRequest request = pageContext.getRequest();

      attributeNames = request.getAttributeNames();
      nameIterator = parameterNames.iterator();
      while (nameIterator.hasNext())
      {
        currentName = nameIterator.next();
        attributeValue = request.getAttributeValue(currentName);

        pageOut.print("<div><span>Name: <span>");
        pageOut.print(currentName);
        pageOut.print("</span></span><span>Values: ");
        pageOut.print("<span>");
        pageOut.print(attributeValue.toString());
        pageOut.print("</span>");

        pageOut.print("</span></div>");
      }
    }
  }

You can use pagecontext.getAttributeNamesInScope(int scope) to get the attributes for each scope as well. The scopes (defined in the PageContext class) are APPLICATION_SCOPE, PAGE_SCOPE, REQUEST_SCOPE, and SESSION_SCOPE.

Pearlinepearlman answered 5/8, 2011 at 13:48 Comment(2)
Add some detail to your question. Which what?Pearlinepearlman
How do I dump the page-scope for example?Vermicelli
M
0

I do as DwB said and now I can give more details about how to dump the variables in the jstl.

first create a custom tag,here is the code.

public class JSTLElDumpTag extends TagSupport {
    @Override
    public int doStartTag() throws JspException {
        JspWriter out=pageContext.getOut();
        try{
            //out request attribute
            ServletRequest request=pageContext.getRequest();
            Enumeration it=request.getAttributeNames();
            out.print("<div><h2>request scope</h2>");
            while(it.hasMoreElements()){
                Object next=it.nextElement();
                out.print("<div>"+next+":"+request.getAttribute(next.toString())+",value type:"+request.getAttribute(next.toString()).getClass().getName()+"</div>");
            }
            out.print("</div>");
            return super.doStartTag();
        } catch (IOException e){
            throw new JspException("Io exception occurred ");
        }
    }
}

in the code above I dumped all the request variables,include its name,value and type. I think type is very important when we dealing with numbers.

next we need to configure our tld file.

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>date</short-name>
<!-- Invoke 'Generate' action to add tags or functions -->
<tag>
    <name>eldump</name>
    <tag-class>JSTLElDumpTag</tag-class>
    <body-content>JSP</body-content>
</tag>

configure this tld file in your web.xml

<jsp-config>
    <taglib>      
    <taglib-uri>/tags</taglib-uri> 
    <taglib-location>/WEB-INF/datetag.tld</taglib-location>          
    </taglib> 
</jsp-config>

the tld file name is datetag.tld

now we can use in our jsp file

<%@taglib prefix="bw" uri="/tags" %>

put above in the head of your jsp file,and in the end of your jsp file you use

<bw:eldump></bw:eldump>

to dump your variables then.

What I should declare is that in some cases we need to dump the variables in the jsp file which were declared by the jstl tag cset , you should add attribute scope=request when you set variables or the manner above will not dump these variables.

I hope this would help you and if you find some errors in my post,your notice will sincerely appreciated.

Miler answered 24/8, 2016 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.