【问题标题】:h:selectManyListBox setter not setting all the values selectedh:selectManyListBox 设置器未设置所有选定的值
【发布时间】:2013-04-29 00:49:16
【问题描述】:
<h:selectManyListbox id="sectorsListBox"  size="2" multiple="multiple" value="#{Mybean.classificationSelectedItems}">
      <f:selectItems id="sectors" value="#{Mybean.classificationSelectItems}"/>
</h:selectManyListbox>

Backing Bean 有:

public class Mybean
{
private Map<String,String> classificationSelectItems = new LinkedHashMap<String,String>();
private List<String> classificationSelectedItems = new ArrayList<String>();

//getter and setter for both. 
}
init()
{
 classificationSelectItems.put("INS","Insurance")
 classificationSelectItems.put("HLC","HealthCare")
}

多选框使用这 2 个值初始化,但问题是只有最后选择的条目存储在分类选择项中。为什么呢 ?以及如何获取存储在 classificationSelectedItems 列表中的所有选定条目?

添加仅供参考,init方法是Spring类。

【问题讨论】:

  • 我刚刚用示例进行了测试,它工作正常(我使用 List alter String[]):mkyong.com/jsf2/jsf-2-multiple-select-listbox-example
  • 如果您同时选择保险和医疗保健,是否都在您的分类选择项目中进行设置?
  • 能贴出代码吗?
  • 当然,他们两个。

标签: jsf


【解决方案1】:

我用一个例子测试过(参考:http://www.mkyong.com/jsf2/jsf-2-multiple-select-listbox-example/),祝你好运:)

Facelets:

<h:form id="form">
        <h:selectManyListbox value="#{user.favFood1}" >
            <f:selectItems value="#{user.favFood2Value}" />
        </h:selectManyListbox>
        <h:commandButton value="test"/>
    </h:form>

豆:

@ManagedBean(name = "user")
@ViewScoped
public class UserBean implements Serializable {

    private static final long serialVersionUID = 1L;
    public List<String> favFood1;
    private Map<String, Object> food2Value;

    public UserBean() {
        favFood1 = new ArrayList<String>();
        food2Value = new LinkedHashMap<String, Object>();
        food2Value.put("Food2 - Fry Checken", "Fry Checken1"); //label, value
        food2Value.put("Food2 - Tomyam Soup", "Tomyam Soup2");
        food2Value.put("Food2 - Mixed Rice", "Mixed Rice3");
    }

    public List<String> getFavFood1() {
        return favFood1;
    }

    public void setFavFood1(List<String> favFood1) {
        this.favFood1 = favFood1;
    }

    public Map<String, Object> getFavFood2Value() {
        return food2Value;
    }
}

【讨论】:

  • 我也是,我正在使用 mojarra 2.1.21 :)
【解决方案2】:

当我在 setter 方法中使用 Collection 时,我注意到了这种行为,比如

public void setClassificationSelectedItems(Collection<String> in){
    // store it somewhere
}

在恢复阶段而不是在更新阶段调用此设置器,因此将设置先前设置的值,但不会设置新值。如果您使用List,它会按预期工作:

public void setClassificationSelectedItems(List<String> in){
    // store it somewhere
}

请注意,您需要在进行此类更改后重新部署应用程序,因为需要重新编译 JSP,但这不会自动完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多