如何使用两个ArrayList的SelectManyCheckbox? - Primefaces

Pal*_*ini 3 jsf primefaces selectmanycheckbox

我正在尝试实施一个,<p:selectManyCheckbox>但我没有成功.

现在我有以下架构:

Course - have many Disciplines
Discipline - belongs to none, one or many Courses.
Run Code Online (Sandbox Code Playgroud)

Course课堂上我有两个ArrayList<Discipline>:

public class CourseMBean{

    (...)
    // Stores all disciplines
    private static ArrayList<Discipline> allDisciplines;

    // Stores only the disciplines that's already associated with this course.
    private static ArrayList<Discipline> courseDisciplines;

    (get and set for the arraylists)
    (...)
}
Run Code Online (Sandbox Code Playgroud)

所有数据都来自MYSQL数据库,但这不是问题.现在我想创建一个新课程,所以我在courseDisciplines中没有任何内容.

我希望在复选框中显示allDisciplines,并希望在用户选择一个复选框时,在courseDisciplines中添加此复选框的对象Discipline - 并且当取消选中一个复选框时,从courseDsiciplines中删除该规则.

我的JSF 2.0代码如下:

<p:selectManyCheckbox id="disciplines" value="#{courseMBean.allDisciplines}" layout="grid" columns="2">                                          
     <f:selectItems value="#{courseMBean.courseDisciplines}" />
</p:selectManyCheckbox>
Run Code Online (Sandbox Code Playgroud)

这实际上显示所有学科没有任何选中的复选框,什么是正确的.但是当我选择一些复选框并提交表单时,我尝试在courseDisciplines中打印元素,这不会在控制台中显示任何内容.

我做错了什么?

Bal*_*usC 9

当我选择一些复选框并提交表单时,我尝试在courseDisciplines中打印元素

由于courseDisciplines实际上代表的是可用项而不是所选项,因此您似乎误解了围绕UISelectManyUISelectItem(s)组件的一些基本概念.

<f:selectItem(s)>(从UISelectItem(s)家庭)表示可用的物品.正是UI中显示的那些项目以及最终用户必须选择的项目.

(来自家庭,类似和)的value属性表示(预)选定的项目.如果在首次显示表单时该值为null或为空,则不会预先选择任何内容.或者,如果这包含一些预选项目,则只预选那些可用的项目.<p:selectManyCheckbox>UISelectMany<h:selectManyCheckbox><h:selectManyMenu>equal()

当最终用户更改了UI中的选择并提交表单时,所有选定的项目将最终出现在组件的value属性中UISelectMany.将UISelectItem(s)保持不变.

这是一个基本的启动示例:

<p:selectManyCheckbox value="#{bean.selectedItems}">
    <f:selectItems value="#{bean.availableItems}" />
</p:selectManyCheckbox>
<p:commandButton value="Submit" action="#{bean.submit}" />
<p:messages autoUpdate="true" />
Run Code Online (Sandbox Code Playgroud)
private List<String> selectedItems; // +getter +setter
private List<String> availableItems; // +getter (no setter necessary!)

@PostConstruct
public void init() {
    availableItems = new ArrayList<String>();
    availableItems.add("one");
    availableItems.add("two");
    availableItems.add("three");
}

public void submit() {
    System.out.println("Selected items: " + selectedItems);
}
Run Code Online (Sandbox Code Playgroud)

(所有其他<p:selectManyXxx><h:selectManyXxx>组件的工作方式完全相同)

当一个复杂的Javabean对象Discipline进入图片时,你需要确保有一个Converterfor,以便JSF可以在它之间正确转换并String在生成的HTML输出中使用,并作为HTTP请求参数(HTML和HTTP即不能传递或保存Java对象,但只包含Java中表示的字符序列String.

这可能是你的根本问题.你说在提交时没有打印到控制台.但submit()实际上永远不会调用整个方法的情况可能同样出色.你对此不够明确.如果确实从未调用整个action方法(即调试断点没有在那里命中,或者另一个System.out.println()打印静态字符串从未在控制台中显示),那么你实际上很可能是转换错误.如果您使用<h|p:message(s)>了正确的方法,或者已经注意了有关排队但未显示的面部消息的服务器日志,那么您应该注意到它.

在这种情况下,您需要实现一个ConverterDiscipline和之间转换的String.

@FacesConverter(forClass=Discipline.class)
public class DisciplineConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        // Write code here to convert from String to Discipline.
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
        // Write code here to convert from Discipline to String.
    }

}
Run Code Online (Sandbox Code Playgroud)

DB ID经常被用作String表示.另请参阅此答案的"复杂对象作为所选项目"部分的相关问题:如何从数据库填充h:selectOneMenu的选项?