Primefaces DataTable + LazyDataModel不会在请求范围内传递setPropertyActionListener [英] Primefaces DataTable+LazyDataModel won't pass setPropertyActionListener in request scope

查看:73
本文介绍了Primefaces DataTable + LazyDataModel不会在请求范围内传递setPropertyActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于LazyDataModel的Primefaces DataTable。我成功地设法将数据加载到表中,但是我无法使用setPropertyActionListener在对话框中显示所选项目或编辑行。我正在使用请求范围,但我注意到这个问题似乎可以在会话范围内解决,但我宁愿使用前者(View范围甚至不会在表中加载数据)。我已经尝试重写getRowData和getRowKey方法,但是它们甚至都没有被调用。当我不使用惰性模型方法时,我就不会遇到这些问题。

I have a Primefaces DataTable based on LazyDataModel. I successfully managed to load data in the table but I am not being able to use setPropertyActionListener to show the selected items in a Dialog nor to edit rows. I am using request scope and I noticed this issue seems to be solved in session scope, but I would rather use the former (View scope won't even load data in the table). I already tried to override getRowData and getRowKey methods but they don't even get called. I have none of this issues when I don't use the lazy model approach.

这可能是与注射相关的问题,我想不见了,否则LazyDataModel不能在外部工作会话范围?

Could this be a injection related issue that I am missing or LazyDataModel won't work outside session scope?

public class LazyDataModelBase<T> extends LazyDataModel<T> {

private volatile AbstractFacade<T> facade;

public LazyDataModelBase(AbstractFacade<T> facade) {
    super();
    this.facade = facade;
}

public final AbstractFacade<T> getFacade() {
    return facade;
}

public final void setFacade(AbstractFacade<T> facade) {
    this.facade = facade;
}

@Override
public List<T> load(int first, int pageSize, String sortField,
        SortOrder sortOrder, Map<String, String> filters) {
    List<T> list = facade.load(first, pageSize,
            sortField, sortOrder, filters);
    if (list == null) {
        throw new RuntimeException("Problem.");
    }
    // I am using the following line for debugging:
    // throw new RuntimeException(list.toString());
    setRowCount(facade.count());
    return list;
}

}

UserLazyDataModel看起来像这样

UserLazyDataModel looks like this

public class UserLazyDataModel extends LazyDataModelBase<User> {

    private UserFacade facade;

    public UserLazyDataModel(UserFacade facade) {
        super(facade);
    }

    public List<User> getRowData(String rowKey) {
        return facade.findById(Integer.parseInt(rowKey)); // findById as defined in UserFacade
    }

    ...

我的后备bean看起来像这样

My backing bean looks like this

@Named(value = "userController")
@RequestScoped
public class UserController implements Serializable {

    private User current;
    @EJB
private UserFacade ejbFacade;
private LazyDataModelBase<User> items;
private Integer itemId;
private int pageSize = 10;

public UserController() {
    items = new UserLazyDataModel(ejbFacade);
}

public UserFacade getUserFacade() {
    return ejbFacade;
}

public void setUserFacade(UserFacade facade) {
    this.ejbFacade = facade;
    //items.setFacade(facade);
}

public User getSelected() {
    if (current == null) {
        this.current = new User();
    }
    return current;
}

public void setSelected(User selected) {
    this.current = selected;
}

public void setSelected(Integer id) {
    this.itemId = id;
    current = ejbFacade.find(id);
}

public Integer getItemId() {
    return itemId;
}

public void setItemId(Integer id) {
    this.itemId = id;
    current = ejbFacade.findById(id);
}

public int getPageSize() {
    return pageSize;
}

public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}

private void setDefaultStatus() {
    current.setStatusId(ejbFacade.getDefaultStatus());
}

public String create() {
    current.setDateCreated(new Date());
    if (current.getStatusId() == null) {
        setDefaultStatus();
    }
    current.hashPassword();

    try {
        ejbFacade.create(current);
        JsfUtil.addSuccessMessage("User guardado exitosamente");
        return "success-create";
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "A persistence error ocurred.");
        return null;
    }
}

public String prepareList() {
    //current = null;
    return "index?faces-redirect=true";
}

public String prepareCreate() {
    return "create?faces-redirect=true";
}

public String prepareEdit() {
    return "edit";
}

public void onRowSelect() {
    current = ejbFacade.findById(itemId);
}

public void update() {
    try {
        ejbFacade.edit(current);
        JsfUtil.addSuccessMessage("User guardado exitosamente");
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "A persistence error ocurred.");
    }
}

public void delete() {
    try {
        ejbFacade.remove(current);
        current = null;
        JsfUtil.addSuccessMessage("role deleted");
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
    }
}

public LazyDataModelBase<User> getItems() {
    items.setFacade(ejbFacade);
    items.setPageSize(pageSize);
    return items;
}

public void setItems(LazyDataModelBase<User> model) {
    this.items = model;
}

}

这是我的观点

    <h:form prependId="false">
        <p:commandButton immediate="true" style="margin: 10px 0;" value="Nuevo" icon="ui-icon-plus" action="#{userController.prepareCreate}" ajax="false" />
    </h:form>
    <h:form id="listForm">
        <br />
        <p:growl id="messages" showDetail="true"/>
        <p:dataTable var="item" value="#{userController.items}" rows="#{userController.pageSize}"
                     lazy="true" selectionMode="single" rowKey="#{item.userId}" selection="#{userController.selected}"
                     paginator="true" id="itemsTable"  rowsPerPageTemplate="10,30,50">


            <p:ajax event="rowSelect" listener="#{userController.onRowSelect}" update=":listForm:itemDisplay" oncomplete="itemDialog.show()" />

            <p:column headerText="ID" id="userId" sortBy="#{item.userId}" style="width: 10px;">
                #{item.userId}
            </p:column>

            <p:column headerText="First Name" id="first-name" sortBy="#{item.firstName}">
                #{item.firstName}
            </p:column>

            <p:column headerText="Last Name" id="last-name" sortBy="#{item.lastName}">
                #{item.lastName}
            </p:column>

            <p:column headerText="Email" id="email" sortBy="#{item.email}" filterBy="#{item.email}">
                #{item.email}
            </p:column>

            <p:column headerText="User" id="name" sortBy="#{item.username}">
                #{item.username}
            </p:column>

            <p:column headerText="birthdate" id="birthdate" sortBy="#{item.birthdate}">
                <h:outputText value="#{item.birthdate}" >
                    <f:convertDateTime type="date" pattern="yyyy/MM/dd"/>
                </h:outputText>
            </p:column>

            <p:column headerText="role" id="status" sortBy="#{item.statusId.name}">
                #{item.statusId.name}
            </p:column>



            <p:column style="width:32px;">
                <h:panelGrid columns="3" class="datarow-icons">
                    <p:commandButton icon="ui-icon-trash" update=":listForm:itemsTable :listForm:confirmDelete"
                                     oncomplete="deleteDialog.show()">
                        <f:setPropertyActionListener value="#{item}" target="#{userController.selected}" />
                    </p:commandButton>

                    <p:commandButton icon="ui-icon-pencil" immediate="true" action="#{userController.prepareEdit}" ajax="false">
                        <f:setPropertyActionListener value="#{item}" target="#{userController.selected}" />
                    </p:commandButton>

                    <p:commandButton id="select-button" 
                                     update=":listForm:itemDisplay" 
                                     oncomplete="itemDialog.show()" icon="ui-icon-search"
                                     title="view" >
                        <f:setPropertyActionListener value="#{item}" target="#{userController.selected}" />
                    </p:commandButton>
                </h:panelGrid>
            </p:column>
        </p:dataTable>
        <p:dialog widgetVar="itemDialog" resizable="false"
                  showEffect="explode" hideEffect="explode" header="Item Detail">
            <p:panelGrid columns="2" id="itemDisplay" style="width: 250px;">
                <h:outputText value="ID: " />
                <h:outputText value="#{userController.selected.userId}" />

                <h:outputText value="Name: " />
                <h:outputText value="#{userController.selected.username}" />

                <h:outputText value="Description: " />
                <h:outputText value="#{userController.selected.firstName}" />
            </p:panelGrid>
        </p:dialog>
        <p:confirmDialog id="confirmDelete" header="Confirm delete" 
                         widgetVar="deleteDialog" severity="alert"
                         showEffect="fade" hideEffect="fade"
                         closable="false"
                         message="¿Realmente desea borrar '#{userController.selected.toString()}' ?">
            <p:commandButton id="decline" value="Not Yet" 
                             onclick="deleteDialog.hide();" type="button" />
            <p:commandButton id="accept" value="Confirmar" 
                             update=":listForm" 
                             oncomplete="deleteDialog.hide();"
                             action="#{userController.delete}" ajax="false">
            </p:commandButton>
        </p:confirmDialog>
    </h:form>


推荐答案

我设法通过覆盖getRowData来使rowSelect正常工作我的UserLazyDataModel(我显示的LazyDataModelBase的子类)如下:

I managed to get rowSelect to work by overriding getRowData in my UserLazyDataModel (a subclass of LazyDataModelBase I showed) as follows:

@Override
public User getRowData(String rowKey) {
    try {
        return ((UserFacade) getFacade()).findById(Integer.parseInt(rowKey));
    } catch (Exception e) {
        return null;
    }

}

并删除

rowkey="#{item.userId}"

dataTable声明中的属性。我在子类UserLazyDataModel中声明了一个Facade,默认情况下,它与父类LazyDataModelBase不同(设置为null)。解决方案是从子类中删除该字段,并通过getFacade方法访问父字段,最后将其强制转换为UserFacade。

attribute from the dataTable declaration. I declared a facade in the subclass UserLazyDataModel and this was by default different from parent class LazyDataModelBase (set to null). The solution was to delete this field from the subclass and access parent fiel by means of getFacade method finally casting it to UserFacade.

setPropertyActionListener尚不可用。

setPropertyActionListener not working yet.

这篇关于Primefaces DataTable + LazyDataModel不会在请求范围内传递setPropertyActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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