【问题标题】:Why is listShuttle component in richFaces not getting updated?为什么richFaces 中的listShuttle 组件没有更新?
【发布时间】:2011-01-24 16:25:08
【问题描述】:

我是 Richfaces 组件的新手。当我使用<rich:listShuttle> 时,targetValue 中指定的 Arraylist 现在正在使用最新数据进行更新?

MyJSF 文件

<a4j:region>
<rich:listShuttle sourceValue="#{bean.selectItems}" id="one"
 targetValue="#{bean.selectItemsone}" var="items" listsHeight="150"
 sourceListWidth="130" targetListWidth="130"
 sourceCaptionLabel="Intial Items"
 targetCaptionLabel="Selected Items" converter="Listconverter">
    <rich:column>
        <h:outputText value="#{items.value}"></h:outputText>
    </rich:column>
</rich:listShuttle>
</a4j:region>
<a4j:region>
<a4j:commandButton value="Submit" action="#{bean.action}" />
</a4j:region>

我的托管 Bean

private List<String> selectedData;
private List<BeanItems> selectItems;
private List<BeanItems> selectItemsone;

public String action() {
    System.out.println(selectItems);
    System.out.println(selectItemsone);
    System.out.println("Select Item List");
    Iterator<BeanItems> iterator = selectItems.iterator();
    while (iterator.hasNext()) {
        BeanItems item = (BeanItems) iterator.next();
        System.out.println(item.getValue());
    }
    System.out.println("/nSelect Item one list ");
    Iterator<BeanItems> iterator2 = selectItemsone.iterator();
    while (iterator2.hasNext()) {
        BeanItems item = (BeanItems) iterator2.next();
        System.out.println(item.getValue());
    }
    return "";
}

public void setSelectedData(List<String> selectedData) {
    this.selectedData = selectedData;
}

public List<String> getSelectedData() {
    return selectedData;
}

/**
 * @return the selectItems
 */
public List<BeanItems> getSelectItems() {
    if (selectItems == null) {
        selectItems = new ArrayList<BeanItems>();
        selectItems.add(new BeanItems("value4", "label4"));
        selectItems.add(new BeanItems("value5", "label5"));
        selectItems.add(new BeanItems("value6", "label6"));
        selectItems.add(new BeanItems("value7", "label7"));
        selectItems.add(new BeanItems("value8", "label8"));
        selectItems.add(new BeanItems("value9", "label9"));
        selectItems.add(new BeanItems("value10", "label10"));

    }
    return selectItems;
}

/**
 * @return the selectItemsone
 */
public List<BeanItems> getSelectItemsone() {
    if (selectItemsone == null) {
        selectItemsone = new ArrayList<BeanItems>();
        selectItemsone.add(new BeanItems("value1", "label1"));
        selectItemsone.add(new BeanItems("value2", "label2"));
        selectItemsone.add(new BeanItems("value3", "label3"));
    }
    return selectItemsone;
}

我的转换器类

public Object getAsObject(FacesContext context, UIComponent component,String value) {  
             int index = value.indexOf(':');  
             return new BeanItems(value.substring(0, index), value.substring(index + 1));  
         }  

public String getAsString(FacesContext context, UIComponent component,Object value) {  
             BeanItems beanItems = (BeanItems) value; 
             return beanItems.getValue() + ":" + beanItems.getData();  
        }  

我的 BeanItems 类

private String data;  //Getter & setter
private String value; //Getter & setter

public BeanItems() {

}

public BeanItems(String value, String data) {
    this.value = value;
    this.data = data;
}
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((data == null) ? 0 : data.hashCode());
    result = prime * result + ((value == null) ? 0 : value.hashCode());
    return result;
}

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    final BeanItems other = (BeanItems) obj;
    if (data == null) {
        if (other.data != null)
            return false;
    } else if (!data.equals(other.data))
        return false;
    if (value == null) {
        if (other.value != null)
            return false;
    } else if (!value.equals(other.value))
        return false;
    return true;
}

【问题讨论】:

  • 我没有丰富的 RichFaces 实践经验,所以我可能错了,但不应该都在 same a4j:region?
  • 当i但所有组件都在同一区域内时,不调用为命令按钮指定的动作方法,就像按钮本身处于非活动状态一样。
  • 感谢 daedlus 和 BalusC 的解答和提示,问题已解决

标签: java jsf richfaces


【解决方案1】:

如果您的问题是目标列表未填充,那么我认为您应该覆盖包装对象 [BeanItem] 的 equals 哈希码方法,因为在转换器中,您每次都在 getAsObject 方法中构造新对象。

还可以尝试在您的页面中放置一个包含在 --a4j:outputPanel ajaxRendered="true"-- 中的 h:message 标记,以打印可能生成的任何转换错误。

【讨论】:

  • @daedlus :我已经覆盖了哈希码和 equals 方法,我已经在问题中添加了我的 BeanItem 类。
  • 如果我在覆盖该方法时做错了什么,请提及并感谢您的建议
  • @daedlus:感谢您的回答,它现在正在工作。即使命令按钮在区域内或区域外
  • @Hari:您能否详细说明您是如何解决这个问题的?你的代码出了什么问题?
  • @Iravanchi : 执行操作时出现了一些验证错误,我使用 outputmessage 组件找到了错误并进行了相应的纠正。
猜你喜欢
  • 2014-07-30
  • 2017-05-23
  • 2020-01-30
  • 2018-07-03
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
相关资源
最近更新 更多