【发布时间】:2014-01-11 23:52:43
【问题描述】:
和primefaces。
我有一个简单的用户数据表。我想要每一行“添加项目”中的一个按钮,该按钮导航到用户可以输入项目详细信息的新页面。创建新项目时必须使用关闭客户的 ID。
<p:dataTable id="dataTable" var="customer"
value="#{customerController.lazyCustomerModel}"
rowKey="#{customer.id}" styleClass="userDataTableStyle"
paginator="true" rows="10"
selection="#{customerController.selectedCustomers}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
lazy="true" rowsPerPageTemplate="10,15,50">
<p:column selectionMode="multiple" style="width:18px" />
<p:column>
<f:facet name="header">
<h:outputText value="Id" />
</f:facet>
<h:outputText value="#{customer.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Phone" />
</f:facet>
<h:outputText value="#{customer.phone}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{customer.name}" />
</p:column>
<p:column>
<!-- Here a button to new page for adding item -->
</p:column>
<f:facet name="footer">
<p:commandButton value="Delete Users"
actionListener="#{customerController.deleteUsers}"
update="dataTable" icon="ui-icon-trash" />
</f:facet>
</p:dataTable>
执行此操作的嵌套方法是什么...我可以直接导航到 url 中带有客户 ID 的新页面吗?或者我应该使用 id 提交给控制器然后重定向?
更新:
所以我尝试使用 viewParam : 当我单击“新建项目”按钮时,URL 中没有传递任何参数。
如果我手动输入网址:比如 /newItem.jsf?customerId=2
方法
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
在 NewItemController 中调用 id 为 2 然后我在新项目表单中输入数据并按下保存按钮,但现在 customerId 为空
customers.xhtml
<ui:define name="content">
<f:metadata>
<f:viewParam name="customerId" value="#{customerController.customerEnt.id}" />
</f:metadata>
<h:form id="customers" prependId="false" includeViewParams="true">
<p:panelGrid styleClass="panelGridCenter">
<f:facet name="header">
<p:row>
<p:column style="text-align: center;" colspan="2">Search Customer</p:column>
</p:row>
</f:facet>
// Rows with search fields
<f:facet name="footer">
<p:row>
<p:column style="text-align: center;" colspan="2">
<p:commandButton value="Search"
action="#{customerController.search}"
update=":customers:dataTable" />
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
<br />
<h:outputText
value="Result is more than 100, only the first 100 results are displayed!"
rendered="#{customerController.searchResultSize > 100}" />
<h:outputText value="#{customerController.searchResultSize}" />
<br />
<p:dataTable id="dataTable" var="customer"
value="#{customerController.customers}" rowKey="#{customer.id}"
styleClass="userDataTableStyle" paginator="true" rows="10"
selection="#{customerController.selectedCustomers}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
lazy="true" rowsPerPageTemplate="10,15,50">
// Columns
<p:column>
<p:commandButton value="New Item" action="#{customerController.newItem()}"/>
</p:column>
</p:dataTable>
</h:form>
</ui:define>
客户控制器 这里只展示重要的部分......
@SuppressWarnings("serial")
@SessionScoped
@Named
public class CustomerController implements Serializable {
private Long customerId;
public String newItem() {
return "newItem?faces-redirect=true&includeViewParams=true";
}
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
}
newItem.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/templates/default.xhtml">
<div class="center">
<ui:define name="content">
<f:metadata>
<f:viewParam name="customerId" value="#{newItemController.customerId}" />
</f:metadata>
<h:form id="item">
<p:messages id="messages" />
<p:panelGrid styleClass="panelGridCenter">
...
</p:panelGrid>
</h:form>
</ui:define>
</div>
</ui:composition>
新项目控制器
@SuppressWarnings("serial")
@ViewScoped
@Named
public class NewItemController implements Serializable {
private String customerId;
public void save() throws Exception {
try {
CustomerEnt customerEnt = null;
if (StringUtils.isNotBlank(customerId)) {
customerEnt = customerDas.find(Long.parseLong(customerId));
} else {
throw new Exception("Customer id not set when creating a new item");
}
itemEnt.setCustomerEnt(customerEnt);
serviceSLSB.save(itemEnt);
} catch (Exception e) {
String errorMessage = getRootErrorMessage(e);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
facesContext.addMessage(null, m);
}
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
}
【问题讨论】:
标签: jsf primefaces datatable