如何将SelectManyCheckbox与两个ArrayList一起使用? -Primefaces [英] How to use SelectManyCheckbox with two ArrayList? - Primefaces

查看:158
本文介绍了如何将SelectManyCheckbox与两个ArrayList一起使用? -Primefaces的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现 <p:selectManyCheckbox> ,但是我没有成功.

I'm trying to implement a <p:selectManyCheckbox> but I'm having no success.

现在我具有以下架构:

Course - have many Disciplines
Discipline - belongs to none, one or many Courses.

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)
    (...)
}

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

All data comes from a MYSQL DB, but that isn't the question. Now I want create a new Course, so I don't have anything in courseDisciplines.

我想在复选框中显示allDisciplines,并希望在用户选中一个复选框时,将此复选框的对象Discipline添加到courseDisciplines中;而当取消选中一个复选框时,请从courseDsiciplines中删除该学科.

I want show allDisciplines in checkboxes, and want that when user select one checkbox, the object Discipline of this checkbox be added in courseDisciplines - and when unselect one checkbox, remove the discipline from the courseDsiciplines.

我的JSF 2.0代码如下:

<p:selectManyCheckbox id="disciplines" value="#{courseMBean.allDisciplines}" layout="grid" columns="2">                                          
     <f:selectItems value="#{courseMBean.courseDisciplines}" />
</p:selectManyCheckbox>

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

This actually shows all disciplines without any selected checkboxes, what's right. But when I select some checkboxes and submit the form I try to print the elements inside courseDisciplines, and this don't show anything in console.

我在做什么错了?

推荐答案

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

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

As the courseDisciplines actually represents the available items not the selected items, it seems that you misunderstood some basic concepts around the UISelectMany and UISelectItem(s) components.

<f:selectItem(s)>(来自UISelectItem(s)系列)表示可用项目.正是那些在UI中显示的项目,最终用户必须从中选择.

The <f:selectItem(s)> (from the UISelectItem(s) family) represent the available items. It are exactly those items which are shown in the UI and which the enduser has to choose from.

<p:selectManyCheckbox>value属性(来自UISelectMany系列,例如<h:selectManyCheckbox><h:selectManyMenu>)代表(预先)选择的项目.如果在第一次显示表单时该字段为null或为空,则不会预先选择任何内容.或者,如果其中包含一些预选项目,则将仅选择那些equal()可用的项目.

The value attribute of <p:selectManyCheckbox> (from the UISelectMany family, like <h:selectManyCheckbox> and <h:selectManyMenu>) represent the (pre)selected items. If this is null or empty during first display of the form, then nothing is preselected. Or if this contains some preselected items, then only those available items which are equal() will be preselected.

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

When the enduser has changed the selection in the UI and submits the form, then all selected items will end up in the value attribute of UISelectMany component. The UISelectItem(s) remains unchanged.

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

Here's a basic kickoff example:

<p:selectManyCheckbox value="#{bean.selectedItems}">
    <f:selectItems value="#{bean.availableItems}" />
</p:selectManyCheckbox>
<p:commandButton value="Submit" action="#{bean.submit}" />
<p:messages autoUpdate="true" />

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);
}

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

(all other <p:selectManyXxx> and <h:selectManyXxx> components work exactly the same)

当图片中出现诸如Discipline之类的复杂Javabean对象时,则需要确保其中有一个Converter,以便JSF可以在它和String之间正确转换以用于生成的HTML输出和作为HTTP请求参数(HTML和HTTP既不能传递也不能保存Java对象,而只能由String表示的Java字符序列).

When a complex Javabean object like Discipline comes into the picture, then you need to make sure that there's a Converter for that so that JSF can properly convert between it and String for usage in generated HTML output and as HTTP request parameter (HTML and HTTP namely can't pass around nor hold Java objects, but only character sequences which are in Java represented by String).

这也许是您的根本问题.您说提交时没有任何内容打印到控制台.但是,实际上从未调用整个submit()方法的情况可能会很好.您对此还不够明确.如果确实从未调用过整个操作方法(即,未在其中调用调试断点,或者在控制台中从未显示另一个打印静态字符串的System.out.println()),则实际上您很可能发生了转换错误.如果您以正确的方式使用了<h|p:message(s)>,或者已经关注有关已排队但未显示的面孔消息的服务器日志,那么您应该已经注意到它.

This is perhaps your root problem. You said that nothing is printed to the console on submit. But it could be as good the case that the whole submit() method is actually never being invoked. You're not explicit enough on this. If the whole action method is indeed never invoked (i.e. a debug breakpoint doesn't hit there, or another System.out.println() printing a static string is never shown in console), then you've actually most likely a conversion error. If you have used <h|p:message(s)> the right way, or have paid attention to server log about queued but undisplayed faces messages, then you should have noticed it.

在这种情况下,您需要实现一个Converter,它可以在DisciplineString之间进行转换.

In that case, you need to implement a Converter which converts between Discipline and 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.
    }

}

DB ID通常被用作String表示.另请参阅此答案中有关以下问题的作为所选项目的复杂对象"部分:

More than often the DB ID is being used as String representation. See also the section "Complex object as selected item" of this answer on a related question: How to populate options of h:selectOneMenu from database?

这篇关于如何将SelectManyCheckbox与两个ArrayList一起使用? -Primefaces的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆