【问题标题】:Using SelectManyCheckbox with list of objects将 SelectManyCheckbox 与对象列表一起使用
【发布时间】:2013-12-23 12:33:32
【问题描述】:

我正在尝试创建一个 JSF 页面,让用户选择 X 数量的成分,然后将这些选择的成分保存到列表中。

Ingredient 是一个具有两个值的对象,String IngredientName 和 int ingredientsPrice。

我想要做的是在成分列表中为每个成分创建 1 个选择项(动态大小),然后将所选项目保存到另一个成分列表中。

我尝试了多种不同的方法,但要么我得到了类转换异常,要么复选框根本没有出现。

我的豆子:

@ManagedBean
@SessionScoped
public class ManagedIngredientsBean {

@EJB
IngredientBean iBean;

private List<Ingredient> ingredientList;
private List<Ingredient> checkedOptions;
private List<SelectItem> selectList;

public ManagedIngredientsBean() {

}


public String createNew(){

    ingredientList = iBean.getAllIngredients();
    selectList = new ArrayList<SelectItem>(ingredientList.size());
    for(Ingredient i : ingredientList){
        selectList.add(new SelectItem(i.getIngredientName()));
    }

    return "createnew.xhtml";
}

public List<SelectItem> getSelectList() {
    return selectList;
}

public void setSelectList(List<SelectItem> selectList) {
    this.selectList = selectList;
}

public List<Ingredient> getCheckedOptions() {
    return checkedOptions;
}

public void setCheckedOptions(List<Ingredient> checkedOptions) {
    this.checkedOptions = checkedOptions;
}

public List<Ingredient> getIngredientList() {
    return ingredientList;
}

public void setIngredientList(List<Ingredient> ingredientList) {
    this.ingredientList = ingredientList;
}

@FacesConverter(value="userConverter")
public static class UserConverter implements Converter {
    public Object getAsObject(FacesContext facesContext,
                              UIComponent component, String value) {
        return value;
    }

    public String getAsString(FacesContext facesContext,
                              UIComponent component, Object o) {
        Ingredient i = (Ingredient) o;
        return i.getIngredientName();

    }
}
}

IngredientBean 用于从持久性数据库中获取成分项并将它们作为列表返回:

@Stateless(name = "IngredientEJB")
public class IngredientBean {

    EntityManagerFactory entFactory;
    EntityManager em;

    public IngredientBean() {
        entFactory = Persistence.createEntityManagerFactory("NewPersistenceUnit");
        em = entFactory.createEntityManager();
    }

    public List<Ingredient> getAllIngredients(){
        TypedQuery<Ingredient> ingQuery = em.createQuery("SELECT i FROM Ingredient i", Ingredient.class);
        List<Ingredient> iList =  ingQuery.getResultList();

        return iList;

    }
}

我的 JSF 页面:

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:head>
    <title>Create New Order</title>
</h:head>

<h:body>
    <h:form>
        <h:selectManyCheckbox value = "#{managedIngredientsBean.checkedOptions}">
                <f:converter converterId="userConverter"/>
                <f:selectItem value = "#{managedIngredientsBean.selectList}" var = "item" itemLabel = "#{item.getIngredientName()}" itemValue = "#{item}"/>
        </h:selectManyCheckbox>
    </h:form>

</h:body>

</html>

我可能遗漏了一些明显的东西,或者只是误解了如何使用 selectManyCheckbox 元素,但我完全不知道如何解决这个问题。感谢任何关于我应该如何实施的答案。 :)

编辑:忘了说,托管 bean 中的 createNew() 方法在上一个 JSF 页面中被调用并重定向到这个页面。

【问题讨论】:

  • 这不是一个老式的论坛。这是一个问答网站。得到答案后,无需在标题中大喊“SOLVED”。只需将答案标记为已接受。该问题将在列表和搜索中显示为已回答(因此,“已解决”)。或者,如果没有一个答案真正有助于理解和解决问题,而您自己想出了一个完全不同的答案,请自己将其发布为真实答案,而不是歪曲问题。
  • 我自己解决了,有时间我会添加解决方案的。只是想我会确保没有人在此期间浪费他们的时间。对不起,如果这是违反礼仪:)
  • 我知道。我要说的是:只需将答案作为答案发布,而不是作为问题发布。否则其他人会偶然发现论坛存在的永恒问题:无法通过搜索快速找到答案。

标签: jsf jsf-2 converter selectmanycheckbox


【解决方案1】:

你的转换器坏了。

首先,它必须是一个 bean,所以不能是 static class

其次,它“应该”是对称的:

x.equals(c.getAsObject(ctx, comp, c.getAsString(ctx, component, x)));“应该”为真。

@FacesConverter(value="userConverter")
public class UserConverter implements Converter 
{
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) 
    {
        return database.loadIngredientByUniqueValue(value);
    }

    public String getAsString(FacesContext facesContext,UIComponent component, Object o) 
    {
        Ingredient i = (Ingredient) o;
        return i.getSomeUniqueValue();
    }
}

【讨论】:

  • 不仅如此,selectList 也坏了。
  • 感谢您的回复。我通过使用数组而不是列表并更改了一些代码来解决了这个问题。稍后我会在这里发布答案:)
猜你喜欢
  • 2012-01-19
  • 2013-11-21
  • 2011-03-01
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 2012-05-21
  • 1970-01-01
相关资源
最近更新 更多