和primefaces。JSF(Primefaces)按钮可用数据表示到新页面,数据为
我有一个简单的数据表与用户。我想在每行“添加项目”中导航到用户可以输入项目详细信息的新页面中的按钮。创建新项目时必须使用客户的编号。
<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>
什么是嵌套的方式来做到这一点...我可以直接导航到新的网页与客户ID在URL?或者我应该向控制器提交id并重定向?
更新:
所以我尝试使用viewParam: 当我点击新建项目按钮,没有参数在URL中传递。
如果我在URL手动键入:像 /newItem.jsf?customerId=2
方法
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
在NewItemController被称为与该ID 2 我然后输入数据新项目形成按保存键,但现在的客户ID为空
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>
CustomerController 只有在这里展示的重要组成部分......
@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>
NewItemController
@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;
}
}