gpt4 book ai didi

java - Primefaces DataTable+LazyDataModel 不会在请求范围内传递 setPropertyActionListener

转载 作者:行者123 更新时间:2023-12-01 04:33:09 26 4
gpt4 key购买 nike

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

这可能是我遗漏的与注入(inject)相关的问题,或者 LazyDataModel 在 session 范围之外无法工作?

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 看起来像这样

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 看起来像这样

@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>

最佳答案

我设法通过重写 UserLazyDataModel(我展示的 LazyDataModelBase 的子类)中的 getRowData 来使 rowSelect 工作,如下所示:

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

}

并删除

rowkey="#{item.userId}"

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

setPropertyActionListener 尚未工作。

关于java - Primefaces DataTable+LazyDataModel 不会在请求范围内传递 setPropertyActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17713709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com