java 如何调试JSTL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6945820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:59:07  来源:igfitidea点击:

How to debug JSTL?

javajstlspring-roosts-springsourcetoolsuite

提问by Hedge

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.

我正在使用 SpringSource Tool Suite(带 Roo)并取得了一些成功。不过让我烦恼的是我不知道如何调试标签库的东西。

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?

我可以做些什么来改善这种情况?

采纳答案by bhagyas

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.

一个例外是 Java Server Pages (JSP) 文件,它会在运行时转换为 servlet(程序代码)。

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.

克服这个问题的最好方法是在获得 XML 或相关标记语言的知识后仔细阅读文档并逐步实现标签。

回答by Venkat

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

这总是很棘手,对于这个问题的未来访问者添加我的 2 美分

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

如果你在Eclipse中的jsp上放一个断点就行

Eclipse debug for JSTL

JSTL 的 Eclipse 调试

回答by DwB

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.

您可以编写一个访问 spring 上下文并转储当前变量的自定义标记。您还可以编写一个(或多个)自定义标签来转储应用程序、会话、页面和请求范围的内容。

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.

看一看Java EE 6 API。查找 SimpleTag 以开始实现标签。这是Java EE 5 教程中自定义标记部分的链接。

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 PageContextclass) are APPLICATION_SCOPE, PAGE_SCOPE, REQUEST_SCOPE, and SESSION_SCOPE.

您也可以使用 pagecontext.getAttributeNamesInScope(int scope) 来获取每个范围的属性。范围(在PageContext类中定义)是 APPLICATION_SCOPE、PAGE_SCOPE、REQUEST_SCOPE 和 SESSION_SCOPE。

回答by Chaojun Zhong

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

我按照 DwB 说的去做,现在我可以提供更多关于如何在 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.

接下来我们需要配置我们的 tld 文件。

<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

在您的 web.xml 中配置此 tld 文件

<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

tld 文件名是 datetag.tld

now we can use in our jsp file

现在我们可以在我们的jsp文件中使用

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

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

把上面放在你的jsp文件的头部,并在你使用的jsp文件的末尾

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

我要声明的是,在某些情况下,我们需要转储jsp文件中由jstl标签cset声明的变量,您应该在设置变量时添加属性scope=request,否则上述方式不会转储这些变量。

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

我希望这会对您有所帮助,如果您发现我的帖子中有错误,我们将真诚地感谢您的通知。