[英]JSF (Primefaces) Button in datatable to new page with data
and primefaces. 和素面。
I have a simple datatable with users. 我与用户有一个简单的数据表。 I want a button in each row "Add Item" that navigates to a new page where the user can input item details.
我想要每行“添加项目”中的按钮导航到新页面,用户可以在其中输入项目详细信息。 Off the id of the customer must be used when creating a new item.
创建新商品时必须使用客户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>
What is the nest way to do this... Can I navigate directly to the new page with the customer id in the url ? 嵌套的方法是什么?我可以直接在URL中使用客户ID导航到新页面吗? or should I submit to the controller with the id and then redirect ?
还是应该使用ID提交给控制器,然后重定向?
Update : 更新:
So I try to use viewParam : When I click the New Item button no parameters are passed in the URL. 所以我尝试使用viewParam:当我单击New Item按钮时,URL中没有传递任何参数。
If I type in the URL manually : like /newItem.jsf?customerId=2 如果我手动输入URL:例如/newItem.jsf?customerId=2
The method 方法
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
in NewItemController is called with the id 2 I then type in data in the new item form an press Save button, but now the customerId is null 在NewItemController中以id 2调用,然后按保存按钮在新项目中键入数据,但现在customerId为null
customers.xhtml 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 Only showing the important part here... 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 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 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;
}
}
You can do both, to pass the parameter to a bean in the controller you can do like this: 您可以同时执行以下操作,将参数传递给控制器中的Bean,您可以像这样进行操作:
<h:commandButton value="Link" action="#{formBean.someAction}">
<f:param name="customerId" value="123456" />
</h:commandButton>
An then on the form bean use @ManagedProperty
: 然后在表单bean上使用
@ManagedProperty
:
@ManagedProperty(value = "#{param.customerId}")
private String customerId;
... needs getters and setters ...
Or you can do it with a link with a direct GET and parameters, and on the target page read the GET parameter and save it on the bean like this (see this post ): 或者,您可以使用带有直接GET和参数的链接来完成此操作,然后在目标页面上读取GET参数并将其保存在Bean中,如下所示(请参见此文章 ):
<f:metadata>
<f:viewParam name="customerId" value="#{formBeanbean.customerId}" />
</f:metadata>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.