简体   繁体   English

如何在我的 JSP 页面中使用 Iterables?

[英]How can I work with Iterables in my JSP pages?

I'm using an API that makes heavy use of Iterables (java.lang.Iterable), but I can't use them directly on my JSP pages because JSTL tags and EL can not process them.我正在使用大量使用 Iterables (java.lang.Iterable) 的 API,但我不能直接在我的 JSP 页面上使用它们,因为 JSTL 标签和 EL 无法处理它们。 Right now, I'm transforming each iterable to a list prior to rendering them.现在,我正在将每个可迭代对象转换为列表,然后再渲染它们。

What would be the cleanest and simplest approach to make it work without previous transformations?在没有先前转换的情况下使其工作的最干净和最简单的方法是什么? Do newest JSTL-Jasper-EL-taglibs-etc.做最新的 JSTL-Jasper-EL-taglibs-etc。 versions support it?版本支持吗? Where can I found that information?我在哪里可以找到这些信息? I don't find anything about it googling...我在谷歌上没有找到任何关于它的信息......

I know I can use Iterable.iterator(), but I can't call that method inside the JSP, only in my controller class, and this is very limiting.我知道我可以使用 Iterable.iterator(),但我不能在 JSP 中调用该方法,只能在我的 controller class 中调用该方法,这是非常有限的。

To access the iterators of your Iterables in EL expressions in your JSTL tags, you could use Java code (in your model, controller or service layer) that wraps your Iterable objects in instances of a class Iter that looks like this (with a simple getter method that follows the Java beans method name convention): To access the iterators of your Iterables in EL expressions in your JSTL tags, you could use Java code (in your model, controller or service layer) that wraps your Iterable objects in instances of a class Iter that looks like this (with a simple getter遵循 Java bean 方法名称约定的方法):

public class Iter<T> {

    private final Iterable<T> iterable;

    public Iter(Iterable<T> iterable) {
        this.iterable = iterable;
    }

    public Iterator<T> getIterator() {
        return iterable.iterator();
    }
}

You can create a Map with your Iterables as a key, and their Iterators as values.您可以创建一个Map ,其中您的Iterables作为键,它们的Iterators作为值。 Then you should be able to access the Iterators using your Iterables in your JSP using JSTL/EL.然后,您应该能够使用 JSTL/EL 使用 JSP 中的Iterables访问Iterators

Further reading:进一步阅读:

You can implement your own JSP tags in Java, so you could write a tag that accepted an Iterable and then use that in the same way as you'd use the JSTL forEach tag.您可以在 Java 中实现您自己的 JSP 标签,因此您可以编写一个接受Iterable的标签,然后以与使用 JSTL forEach标签相同的方式使用它。

This documentation has an example on how to create a custom tag that iterates through an arbitrary custom object - your implementation would likely be simpler.本文档有一个关于如何创建自定义标签的示例,该标签遍历任意自定义 object - 您的实现可能会更简单。 The key is in returning EVAL_BODY_AGAIN to allow you to process the body of the tag multiple times.关键在于返回EVAL_BODY_AGAIN以允许您多次处理标签的主体。

http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPTags6.html#68297 http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPTags6.html#68297

I haven't tried running this, but it might be something to start with:我还没有尝试过运行它,但它可能是从以下开始的:

public class IterableForEachTag extends BodyTagSupport {
    private static final long serialVersionUID = 1L;
    private Iterable<?> iterable = null;
    private Iterator<?> iterator = null;
    private String var = null;

    public int doStartTag() throws JspException {
        if (iterator == null) {
            iterator = iterable.iterator();
        }
        if (iterator.hasNext()) {
            Object element = iterator.next();
            pageContext.setAttribute(var, element);
            return (EVAL_BODY_AGAIN);
        } else
            return (SKIP_BODY);
    }

    public int doAfterBody() throws JspException {
        if (bodyContent != null) {
            try {
                JspWriter out = getPreviousOut();
                out.print(bodyContent.getString());
                bodyContent.clearBody();
            } catch (IOException e) {
                throw new JspException(e);
            }
        }
        if (iterator.hasNext()) {
            Object element = iterator.next();
            pageContext.setAttribute(var, element);
            return (EVAL_BODY_AGAIN);
        } else {
            return (SKIP_BODY);
        }
    }

    public void setIterable(Iterable i) {
        iterable = i;
        iterator = null;
    }

    public void setVar(String v) {
        var = v;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的 JSP 项目更容易让设计师使用 - How can I make my JSP project easier for a designer to work with 如何将我的requestdispatcher发送到两个不同的jsp页面路径? - How can I send my requestdispatcher to two different jsp pages path? 我如何在Intranet服务器中部署Web应用程序(带有JSP页面) - how can i Deploy my web app (with JSP pages) in a intranet server 如何在不编辑的情况下将.js文件包含到项目的所有jsp页面中? - How can I include a .js file to all jsp pages of my project without editing it? 如何使用JSP生成具有非JSP扩展的内容页面? - How can I use JSP to generate content pages with a non-JSP extension? 我可以将DigitalOcean上的JSP页面与LEMP一起使用吗? - Can I use JSP pages on DigitalOcean with LEMP? 如何为我的jsp页面赋予动态性质? - how to give dynamic nature to my jsp pages? 我的网站建立在Java Server Pages(JSP)之上-我仍可以使用Joomla吗? - My website is built on Java Server Pages (JSP) - can I use Joomla still? 如何将JSP页面集成到Web服务中? - How can I integrate my JSP page into my web service? 如何从JSP页面获得登录名? 如何更正我的jsp代码。 - How can I get a Login from JSP page? How can I correct my jsp code.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM