java Primefaces 选项列表目标和源值不会改变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13657042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:31:16  来源:igfitidea点击:

Primefaces picklist target and source values do not change

javajsfprimefacesomnifaces

提问by Silverfish

My primefaces pickList source and target values do not change, I have followed the example in primefaces showcaseand also looked at several posts here but still am not able to solve the problem. I am using a list from the database to populate the source as follows:

我的 primefaces pickList 源和目标值没有改变,我已经按照primefaces 展示中的示例进行了操作,并且还查看了此处的几篇文章,但仍然无法解决问题。我正在使用数据库中的列表来填充源,如下所示:

private DualListModel<Course> courseModel;
public CourseBean() {
    List<Course> target = new ArrayList<Course>();
    List<Course> source = new ArrayList<Course>();
    courseModel = new DualListModel<Course>(source, target);
}
...
//this DualListModel getter also populates the source with values from db
public DualListModel<Course> getCourseModel() {
    courseModel.setSource(getCourseList());
    return courseModel;
}

My converter is

我的转换器是

import org.omnifaces.converter.SelectItemsConverter;

@FacesConverter("courseConverter")
public class CourseConverter extends SelectItemsConverter {
@Override
public String getAsString(FacesContext context, UIComponent component,
        Object value) {
    Integer id = (value instanceof Course) ? ((Course) value).getId()
            : null;
    return (id != null) ? String.valueOf(id) : null;
}
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
    Course course = new Course();
    course.setId(Integer.parseInt(value));
    return course;
}
}

and finally my xhtml page is:

最后我的 xhtml 页面是:

<h:form>
    <p:pickList id="coursesOffered"
        value="#{courseView.courseModel}" var="course"
        itemValue="#{course}" itemLabel="#{course.courseTitle}"
        converter="courseConverter" showSourceFilter="true"
        showTargetFilter="true" filterMatchMode="contains">

        <f:facet name="targetCaption">Selected</f:facet>
        <f:facet name="sourceCaption">All Courses</f:facet>

        <p:ajax event="transfer" listener="#{courseView.onTransfer}" />

        <p:column style="width:10%">  
        #{course.courseCode}:
    </p:column>
        <p:column style="width:90%">  
       #{course.courseTitle}  
    </p:column>

    </p:pickList>

    <p:commandButton id="pojoSubmit" value="Submit"
        update="displayPlayers" oncomplete="playerDialog.show()"
        style="margin-top:5px" />

    <p:dialog showEffect="fade" hideEffect="fade" widgetVar="playerDialog">
        <h:panelGrid id="displayPlayers" columns="2">

            <h:outputText value="Source: " style="font-weight:bold" />
            <ui:repeat value="#{courseView.courseModel.source}"
                var="course">
                <h:outputText value="#{course.courseTitle}"
                    style="margin-right:5px" />
            </ui:repeat>

            <h:outputText value="Target: " style="font-weight:bold" />
            <ui:repeat value="#{courseView.courseModel.target}"
                var="course">
                <h:outputText value="#{course.courseTitle}"
                    style="margin-right:5px" />
            </ui:repeat>

        </h:panelGrid>
    </p:dialog>
</h:form>

The pickList is displayed correctly with source populated with values from database, however, upon clicking the button, the dialog just displays the original source list values and an empty target list even after transferring items on the interface. What am I missing?

选择列表正确显示,源填充了数据库中的值,但是,单击按钮后,即使在界面上传输项目后,对话框也只显示原始源列表值和空目标列表。我错过了什么?

回答by Aksel Willgert

I see some problems with your code. In the getter, you are reconstructing the DualList from the database, reseting any changes you've done so far.

我发现您的代码存在一些问题。在 getter 中,您正在从数据库重建 DualList,重置您迄今为止所做的任何更改。

try making you getter something like this:

试着让你得到这样的东西:

public DualListModel<Course> getCourseModel() {
    return this.courseModel;
}

Construct and load your list from the database in a method annotated @PostConstruct rather than in the constructor+getter.

使用@PostConstruct 注释的方法而不是构造函数+getter 从数据库构造和加载列表。

public CourseBean() {}

@PostConstruct
public void init() {
    List<Course> target = new ArrayList<Course>();
    courseModel.setSource(getCourseList());
    courseModel = new DualListModel<Course>(source, target);
}

Also annotate your bean @ViewScoped, so you dont construct a new bean with empty target-list on every request

还要注释你的 bean @ViewScoped,这样你就不会在每个请求上构造一个带有空目标列表的新 bean

@ManagedBean(name="courseView")
@ViewScoped
public class CourseBean {

}

Finally you also need a setter:

最后你还需要一个 setter:

public void setCourseModel(DualListModel<Course> courseModel) {
    this.courseModel = courseModel;
}

I have not really looked into the converter, when I did a picklist lasttime i took the converter included in primefaces showcase (Never tried the omnifaces one). Here is a link to the source: http://code.google.com/p/ind/source/browse/indicadorCensoJSF/src/ve/gob/ine/censo/beans/convert/PrimeFacesPickListConverter.java?spec=svn154&r=154

我没有真正研究过转换器,当我上次做一个选择列表时,我使用了包含在 primefaces 展示中的转换器(从未尝试过 omnifaces)。这是源的链接:http: //code.google.com/p/ind/source/browse/indicadorCensoJSF/src/ve/gob/ine/censo/beans/convert/PrimeFacesPickListConverter.java?spec=svn154&r= 154

回答by Lúcio José Beir?o

Complementary to the accepted answer, I had a problem with the Omnifaceslist converter. Everything worked, except that the already picked items weren't being updated and "lost", it is, they were there in the view but the only items processed were the picked in the view, not the ones that were already set as targets in the bean, so, instead of having a target list with the picked items plus the ones that were set at the dualList constructor, the target list had just the ones picked at the view, not the previously set items.

作为已接受答案的补充,我在使用Omnifaces列表转换器时遇到了问题。一切正常,除了已经选择的项目没有被更新和“丢失”,也就是说,它们在视图中,但唯一处理的项目是在视图中选择的项目,而不是那些已经设置为目标的项目bean,因此,目标列表没有选择的项目加上在 dualList 构造函数中设置的项目,目标列表只有在视图中选择的项目,而不是先前设置的项目。

As a solution, I also implemented a custom picklist converter for the primefaces component. Now everything works like a charm.

作为解决方案,我还为 primefaces 组件实现了一个自定义选项列表转换器。现在一切都像一个魅力。

I'm not answering specifically the actual question because it has an accepted answer already (and i don't have the points to make a comment). I'm just complementing, as some people may have problems using primefaces 2.3 and omnifaces 3.1. I did search a lot about the problem I was facing, and did not found at least a hint of what was happening.

我没有具体回答实际问题,因为它已经有一个公认的答案(而且我没有资格发表评论)。我只是在补充,因为有些人在使用 primefaces 2.3 和 omnifaces 3.1 时可能会遇到问题。我确实搜索了很多关于我面临的问题的信息,但没有发现至少一点正在发生的事情的暗示。

This site: picklist examplehas a tutorial on how to make the converter, if someone has doubts. ACV's answer is also valid for the converter, although it was already implemented by Silverfish, who asked the question.

如果有人有疑问,这个站点:picklist 示例有一个关于如何制作转换器的教程。ACV 的回答也适用于转换器,尽管它已经由提出问题的 Silverfish 实施。

回答by ACV

To work with objects, you need a converter. This is the best answer how to implement it: https://stackoverflow.com/a/6625392/912829

要处理对象,您需要一个转换器。这是如何实现它的最佳答案:https: //stackoverflow.com/a/6625392/912829