java 使用 JSF、MyFaces 和 Facelets 迭代 HashMap 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852534/
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-29 14:04:10  来源:igfitidea点击:

Troubles Iterating Over A HashMap with JSF, MyFaces & Facelets

javajsffaceletsmyfaces

提问by Lee Theobald

I'm having some trouble looping over a HashMap to print out it's values to the screen. Could someone double check my code to see what I'm doing wrong. I can't seem to find anything wrong but there must be something.

我在遍历 HashMap 以将其值打印到屏幕上时遇到了一些麻烦。有人可以仔细检查我的代码,看看我做错了什么。我似乎找不到任何问题,但一定有什么问题。

In a servlet, I am adding the following to the request:

在 servlet 中,我将以下内容添加到请求中:

Map<String, String> facetValues = new HashMap<String, String>();
// Filling the map
req.setAttribute(facetField.getName(), facetValues);

In one case "facetField.getName()" evaluates to "discipline". So in my page I have the following:

在一种情况下,“facetField.getName()”评估为“纪律”。所以在我的页面中,我有以下内容:

<ui:repeat value="${requestScope.discipline}" var="item">
  <li>Item: <c:out value="${item}"/>, Key: <c:out value="${item.key}"/>, Value: <c:out value="${item.item}"/></li>
</ui:repeat>

The loop is ran once but all the outputs are blank?!? I would have at least expected something in item if it's gone over the loop once. Checking the debug popup for Facelets, discipline is there and on the loop. Printing it to the screen results in something that looks like a map to me (I've shortened the output) :

循环运行一次但所有输出都是空白的?!?如果它循环一次,我至少会期望 item 中有一些东西。检查 Facelets 的调试弹出窗口,纪律就在那里并且在循环中。将它打印到屏幕上的结果对我来说看起来像一张地图(我缩短了输出):

{300=0, 1600=0, 200=0, ... , 2200=0}

I've also tried with a c:forEach but I'm getting the same results. So does anyone have any ideas where I'm going wrong?

我也试过 ac:forEach 但我得到了相同的结果。那么有没有人知道我哪里出错了?

Thanks for any input, Lee

感谢您的任何意见,李

回答by Berkay

with el 2.2 support you can iterate maps like below.

使用 el 2.2 支持,您可以像下面这样迭代地图。

<ui:repeat value="#{myBean.stats.keySet().toArray()}" var="x">
    <h:outputText value="#{myBean.stats.get(x)}" /><br />
</ui:repeat>

回答by Lincoln

<ui:repeat>only accepts List or DataModel, not Sets or Maps. This is on the roadmap for JSF 2.1.

<ui:repeat>只接受 List 或 DataModel,不接受 Sets 或 Maps。这是 JSF 2.1 的路线图。

回答by McDowell

Three things occur to me:

我想到了三件事:

1.

1.

The documentation for ui:repeatdoesn't say it (it only says List), but I see UIRepeat uses DataModelas its model (in the manner of h:dataTable). Mapwill not be automatically wrapped with a DataModeltype - the type is not implicitly supported. You will need to either make the value an instance of your own DataModelimplementation or provide them as an implicitly supported type (e.g. java.util.List).

ui:repeat文档没有说(它只说List),但我看到 UIRepeat 使用DataModel作为它的模型(以 h:dataTable 的方式)。Map不会自动与DataModel类型一起包装- 不隐式支持该类型。您需要使该值成为您自己的DataModel实现的实例,或者将它们作为隐式支持的类型(例如java.util.List)提供。

2.

2.

I am not sure what you intend these values to map to:

我不确定您打算将这些值映射到什么:

${item}
${item.key}
${item.item}

If you change "discipline" to be of type List<Map.Entry<String,String>>, you could bind to the key and valueproperties:

如果将“学科”更改为List<Map.Entry<String,String>> 类型,则可以绑定到键和值属性:

${item.key}
${item.value}

You can create your list like so:

您可以像这样创建列表:

Map<String, String> facetValues = new HashMap<String, String>();
// Filling the map
List<Map.Entry<String, String>> discipline
        = new ArrayList<Map.Entry<String, String>>(facetValues.entrySet());

3.

3.

The FAQ suggeststhat JSTL tags are only evaluated at component tree creation time. It is unclear to me whether using c:out as a child of ui:repeat will work properly. You might need to use h:outputText instead. (I could be wrong about this, of course - I have not tried it.)

FAQ表明该JSTL标签在组件树创建时间只计算。我不清楚使用 c:out 作为 ui:repeat 的子项是否会正常工作。您可能需要改用 h:outputText。(当然,我可能是错的 - 我没有尝试过。)



In a servlet, I am adding the following to the request

在 servlet 中,我将以下内容添加到请求中

That sounds like an odd way to put something into request scope in JSF, but I'll trust that you know what you're doing!

这听起来像是在 JSF 中将某些内容放入请求范围的奇怪方法,但我相信您知道自己在做什么!