打印两个单独的数组作为itemLabel和jsf f中的值:selectItems?

时间:2017-03-26 18:10:01

标签: jsf jsf-2 managed-bean

我的h遇到问题:selectOneMenu和f:selectItems

我想要"价值"从名为list

的数字数组中取出
private double list = {51.1511, 53.51351, 0.634343, 2.52555}

并且每个值的标签都是

private String curr = {PYN, DKT, ALT, BIT}

目前我做

<h:selectOneMenu value="#{serviceBean.select2}">
    <f:selectItems value="#{serviceBean.list}"  itemLabel="#{serviceBean.curr}" />
</h:selectOneMenu>

这可以给我作为数字的值,但不是向我显示项目标签内的字符串,而是显示内存引用。如何让它显示字符串?

2 个答案:

答案 0 :(得分:1)

public class General{
   private double listValue;
   private String curValue;

   public General(String curValue, double listValue){
     this.listValue = listValue;
     this.curValue = curValue;
   } 

   public double getListValue(){ return listValue;}
   public String getCurValue(){ return curValue;}
}

我假设您生成了对象并创建了一个arraylist ArrayList<General> myvalues

ArrayList<General> myvalues = new ArrayList<>();
myvlues.add(new General("PYN",51.112));
...

<h:selectOneMenu value="#{serviceBean.selectedItem}">
<f:selectItems value="#{serviceBean.myvalues}"
                 var="myvalue"
                 itemValue="#{myvalue.listValue}"
                 itemLabel="#{myvalue.curValue}"/>
</h:selectOneMen>

答案 1 :(得分:0)

您应该使用托管bean来访问表示层中的模型包含。

托管bean:

import React from 'react'

class AudioPlayer extends React.Component {
  render() {
    return (
      <div>
        <audio ref="audio_tag" src="./static/music/foo.mp3" controls autoPlay/>
      </div>
    );
  }
}

export default AudioPlayer;

JSF facelet:

@Named
@SessionScoped
public class MyBean
{
  private final List<Double> values = Arrays.asList( 1.1, 2.1, 3.1 );
  private final List<String> labels = Arrays.asList( "AAA", "BBB", "CCC" );
  private Double selectedItem;

  public List<String> getItems()
  {
    return labels;
  }

  public Double getValueAt( int ndx_ )
  { 
    return values.get( ndx_ );
  }

  public String getLabelAt( int ndx_ )
  {
    return labels.get(  ndx_ );
  }

  public Double getSelectedItem()
  {
    return selectedItem;
  }

  public void setSelectedItem( Double selectedItem_ )
  {
    selectedItem = selectedItem_;
  }

}