简体   繁体   English

使用“JSF”或JSTL TAGS迭代java.util.Map的问题

[英]problem with iterate over java.util.Map using “JSF” OR JSTL TAGS

I'm using Richfaces JSF and I want to iterate over an Map<Object,Object> . 我正在使用Richfaces JSF,我想迭代一个Map<Object,Object> I see many examples on Sun forums and other sites but in my case it doesn't work. 我在Sun论坛和其他网站上看到了很多示例,但在我的情况下它不起作用。 Here is my XHTML code: 这是我的XHTML代码:

<c:forEach items="#{order.customOptions}" var="option">
    <h:outputText value="this text does not print" />
    <h:outputText value="#{option.value.name}" />
    <h:outputText value="#{option.value.key}" />
</c:forEach>

The "order" object is of type Order . “order”对象的类型为Order The "customOptios" is of type Map<CustomOption,CustomOptionValue> . “customOptios”的类型为Map<CustomOption,CustomOptionValue> CustomOption,CustomOptionValue Map<CustomOption,CustomOptionValue> When I create an Javascript alert on to print '#{order.customeOptions}' its content is correct, but it does not even enter in c:forEach loop . 当我创建一个Javascript警报打印'#{order.customeOptions}'时,它的内容是正确的,但它甚至没有输入c:forEach loop

Update 1: : I tried a list but it doesn't work. 更新1 ::我尝试了一个列表,但它不起作用。 I used list and got answer in other pages. 我使用了列表并在其他页面中得到了答案。 I also use a4j:poll and some other ajax component is there any problem with them? 我也使用a4j:poll和其他一些ajax组件有什么问题吗?

<c:forEach items="#{order.food.cusomableOptions}" var="option">
    <h:outputText value="this text does not print" />
    <h:outputText value="#{option.title}" />
</c:forEach>

Update 2: Here is output of <h:outputText value="#{order.customOptions}" /> : 更新2:这是<h:outputText value="#{order.customOptions}" />

{model.CustomOption@be8464=model.CustomOptionValue@14e8ac9, 
 model.CustomOption@1ea0c8b=model.CustomOptionValue@78f4, 
 model.CustomOption@24389c=model.CustomOptionValue@3f0bc0, 
 model.CustomOption@a765c=model.CustomOptionValue@3b34ca, 
 model.CustomOption@95868c=model.CustomOptionValue@199de59}

Update 3: when I use it outside of rich:column it works, but when I use it in a rich:dataTable and rich:column tag it doesn't work: 更新3:当我在rich:column之外使用它时它可以工作,但是当我在rich:dataTablerich:column标签中使用它时它不起作用:

<rich:column>
    <f:facet name="header">
        <h:outputText value="xf" />
    </f:facet>
    <c:forEach items="#{order.customOptions}" var="option">
        <p><h:outputText value="option : #{option.key.title}" /></p>
    </c:forEach>
</rich:column>

此博客可能会有所帮助,因为在JSF上使用JSTL标记时会出现一些问题。

If i need to iterate over Map, i use helper class (for example Entry) like below: 如果我需要迭代Map,我使用helper类(例如Entry),如下所示:

public class Entry {
    private String value;
    private String key;

    public Entry(String value, String key) {
        super();
        this.value = value;
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

and method that convert map to List: 和将地图转换为List的方法:

private List<Entry> mapToList(Map<String,String> map) {
    List<Entry> list = new ArrayList<Entry>();
    for(String key: map.keySet()) {
        list.add(new Entry(key, map.get(key)));
    }
    return list;        
}

xhtml: XHTML:

<ui:repeat var="entry" value="#{bean.list}" varStatus="i">
    <div>#{entry.key} : #{entry.value}</div>
</ui:repeat>

Maybe that will help You... or maybe not ;) 也许那会对你有所帮助......或许不会;)

If the loop is not even entered, that indicates that the map is simply empty. 如果甚至没有输入循环,则表示地图只是空的。

You could diagnose that by putting something like 您可以通过放置类似的东西来诊断它

<h:outputText value="map size: #{order.customOptions.size()}" />

in front of the loop tags, but you should really set up an IDE like eclipse or Netbeans and run your code within it so that you can use a real debugger - you'll be able to pin down the problem much easier and quicker that way. 在循环标签前面,你应该真的设置像eclipse或Netbeans这样的IDE并在其中运行你的代码,以便你可以使用真正的调试器 - 你将能够更容易和更快地解决问题。

JSTL and JSF doesn't work seamlessly together in sync as you would intuitively expect from the ordering in the source code. JSTL和JSF不能无缝地同步工作,因为您可以直观地期望源代码中的排序。 Roughly said, JSTL processes the entire page from top to bottom first and then hands the generated output (thus, without any JSTL tags, but with its generated output) over to JSF which in turn processes the entire page from top to bottom again. 粗略地说,JSTL首先从上到下处理整个页面,然后将生成的输出(因此, 没有任何JSTL标签,但是生成的输出)交给JSF,后者又从上到下处理整个页面。

JSF UIData components like h:dataTable and rich:dataTable haven't generated any rows yet at the moment JSTL runs, which cauces that you won't see anything from c:forEach inside a column. 在JSTL运行的时刻,像h:dataTablerich:dataTable这样的JSF UIData组件还没有生成任何行,这表明你不会在列中看到c:forEach任何内容。

To fix this, you should rather use the JSF-supplied iterating components, such as RichFaces' a4j:repeat , or Facelets' ui:repeat , or Tomahawk's t:dataList . 要解决这个问题,您应该使用JSF提供的迭代组件,例如RichFaces'a4j a4j:repeat ,或Facelets'ui ui:repeat ,或Tomahawk的t:dataList They all do less or more the same as the JSTL c:forEach . 它们都与JSTL c:forEach相同或更少。

For the remnant of JSTL tags, only the functions taglib is useful in JSF, all the other taglibs are superflous in a JSF environment since it either provides the same functionality out of the box (JSTL core and format taglibs), or it simply doesn't suit in the MVC ideology (JSTL sql and xml taglibs). 对于JSTL标记的残余, 只有 函数 taglib在JSF中很有用,所有其他标记库在JSF环境中都是超级的,因为它提供了开箱即用的相同功能(JSTL核心和格式标记库),或者它只是没有'适合MVC意识形态(JSTL sql和xml taglibs)。

Maybe you should not mix JSTL:core tags and JSF:h tags. 也许你不应该混合JSTL:核心标签和JSF:h标签。

Regards. 问候。

只需将您的MAP转换为ArrayList,并使用h:dataTable,您的问题就应该解决了。

You should use an equivalent JSF tag. 您应该使用等效的JSF标记。 Have you tried <a4j:repeat/> ? 你试过<a4j:repeat/>吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM