JSF我需要用StringArray填充selectItems,但我需要先对其进行解析 [英] JSF I need to populate selectItems with a StringArray but I need to parse it first

查看:34
本文介绍了JSF我需要用StringArray填充selectItems,但我需要先对其进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出了另一个问题,希望能找到答案,但是我走错了方向.这是供参考的问题(如何将其解析为多值地图?)

I created another question hoping I could find my answer, but I went in the wrong direction. Here is that question for reference( How do I parse this into a MultiValued Map?)

我正在从属性文件中拉出一个字符串数组(使用Apache配置),并且该数组进入了,看起来像这样

I am pulling a String Array out of a property file (using Apache Configuration) and the array comes in and looks like this

name,5486984,189.658.235.215,3.158.68.15,name2,189189,189.658.218.268,3.158.68.13...

这是JSF的一部分

<f:selectItems itemValue="#{mainBean.information}" value="#mainBean.information}" />

这是Java的摘录

Private String [] information;
information = config.getStringArray("information"); // This brings in this string from the property file

我一直在引用此 http://www.horstmann.com/corejsf/refcard .html

因此,我刚刚发布的内容就在这里.它将填充该字符串数组的每个元素.我只希望显示名称",并希望将其他三个值作为实际的值"传递.

So with what I have here, that I just posted. It will populate every element of that string array. I only want the "name" to show up and the three other values to be passed as the actual "value".

我试图制作一个Map对象,该对象是一个字符串和一个字符串数组,但是JSF拒绝使用它.

I tried to make a Map object that is a string and a string array but JSF refuses to take it.

推荐答案

我试图制作一个Map对象,该对象是一个字符串和一个字符串数组,但是JSF拒绝使用它.

A Map应该起作用.您可以在我们的selectOneMenu标签Wiki页面中找到具体示例.映射键成为选项标签,映射值成为选项值.

A Map should work. You can find a concrete example in our selectOneMenu tag wiki page. The map key becomes the option label and the map value becomes the option value.

您应该只意识到,选项值最终会最终出现在HTML响应中,即HTML <option>元素的value属性之内,就像这样

You should only realize that the option value ultimately ends up in the HTML response, inside the value attribute of the HTML <option> element like so

<option value="someValue">

由于HTML实际上是纯文本,因此如果不先转换它们,就无法在HTML中表示非标准Java对象. String[]是非标准的,当您未明确指定转换器时,默认情况下,

As HTML is in fact plain text, you cannot represent non-standard Java objects in HTML without converting them first. A String[] is non-standard and when you don't explicitly specify a converter, then by default the Object#toString() result will be inlined in HTML and it would in case of a String[] look like

<option value="[Ljava.lang.String;@1860045">

确切地讲,选择该选项后,该值又提交给服务器端.但是,由于该值是一个难以理解的字符序列,因此无法使用Java/JSF/EL以任何方式将其转换回原始的String[]值.

Exactly that value is in turn thus submitted to the server side when the option is selected. However, as the value is an unintelligible sequence of characters, it isn't possible to convert it back to the original String[] value using Java/JSF/EL in any way.

除了提供转换器之外,您还可以只传递包含名称的List<String>.

Instead of providing a converter, you can also just pass only a List<String> containing the names around.

<h:selectOneMenu value="#{bean.name}">
    <f:selectItems value="#{bean.names}" />
</h:selectOneMenu>

在处理表单提交时,您可以直接根据配置文件中的提交名称或被记住为bean属性的某些Map取回关联的String[].

When processing the form submit, you could just get the associated String[] back based on the submitted name straight from the configuration file or some Map which is remembered as bean property.

这篇关于JSF我需要用StringArray填充selectItems,但我需要先对其进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆