[英]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.