和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>
执行此操作的嵌套方式是什么...我可以直接导航到网址中包含客户ID的新页面吗?或者我应该使用id提交给控制器然后重定向?
更新:
所以我尝试使用viewParam: 单击“新建项”按钮时,URL中不会传递任何参数。
如果我手动输入网址:就像 /newItem.jsf?customerId=2
方法
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
使用id 2调用NewItemController中的然后我在新项目中键入数据,然后按“保存”按钮,但现在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>
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;
}
}
答案 0 :(得分:1)
您可以同时执行这两项操作,将参数传递给控制器中的bean,您可以这样做:
<h:commandButton value="Link" action="#{formBean.someAction}">
<f:param name="customerId" value="123456" />
</h:commandButton>
然后在表单bean上使用@ManagedProperty
:
@ManagedProperty(value = "#{param.customerId}")
private String customerId;
... needs getters and setters ...
或者您可以使用带有直接GET和参数的链接来执行此操作,并在目标页面上读取GET参数并将其保存在bean上(请参阅此post):
<f:metadata>
<f:viewParam name="customerId" value="#{formBeanbean.customerId}" />
</f:metadata>