JSF 2.2
Primefaces 4.0
JBoss的WildflyJSF的viewAction傳遞PARAM連帶設置,但返回空
從網頁與客戶的名單,並希望爲每一位客戶,用戶可以一鍵添加項目。 當我點擊「新建項目」按鈕時,我被重定向到新的項目頁面。
在URL中的客戶ID
newItem.jsf;jsessionid=Xw7tdljr9f0atzyET2Fy6_WI?customerId=3
我可以調試,在新項目的bean與值3稱爲組客戶ID的方法,好:)
但之後我調試在獲得客戶ID方法被稱爲..現在的客戶ID爲空:(
而且我做了一個syso:
18:10:25312 INFO [標準輸出(默認任務-9)設置搜索g客戶ID 3
因此,客戶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: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">
...
<p:column>
<p:commandButton ajax="false" value="New Item" action="#{customerController.newItem(customer)}"/>
</p:column>
</p:dataTable>
</h:form>
newItem.xhtml
<ui:define name="content">
<f:metadata>
<f:viewParam name="customerId"
value="#{newItemController.customerId}" />
<f:viewAction action="#{newItemController.init()}"/>
</f:metadata>
<h:form id="item" includeViewParams="true">
...
newItemController.java
@SuppressWarnings("serial")
@ViewScoped
@Named
public class NewItemController implements Serializable {
private CustomerEnt customerEnt;
private String customerId;
@PostConstruct
public void init() {
itemEnt = new ItemEnt();
if (customerId == null) {
String message = "Bad request. Please use a link from within the system.";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
return;
}
customerEnt = customerDas.find(Long.parseLong(customerId));
if (customerEnt == null) {
String message = "Bad request. Unknown customer.";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
}
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
System.out.println("Setting customer id " + customerId);
}
}
CustomerController.java
@SuppressWarnings("serial")
@SessionScoped
@Named
public class CustomerController implements Serializable {
private Long customerId;
public String newItem(CustomerEnt customerEnt) {
customerId = customerEnt.getId();
return "newItem?faces-redirect=true&customerId=" + customerId;
}
由於L-雷表示,在init叫了兩次,所以我做的NewItemController這種變化:
public void init() {
System.out.println("In init");
}
@PostConstruct
public void postConstruct() {
itemEnt = new ItemEnt();
System.out.println("In postConstruct");
}
public void loadData() {
if (customerId == null) {
String message = "Bad request. Please use a link from within the system.";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
return;
}
}
public void save() throws Exception {
try {
serviceSLSB.save(Long.parseLong(customerId), itemEnt);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved!", "Item saved successful");
facesContext.addMessage(null, m);
postConstruct();
} catch (ConstraintViolationException e) {
itemEnt.setBid(null);
String errorMessage = getRootErrorMessage(e);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
facesContext.addMessage(null, m);
} catch (Exception e) {
String errorMessage = getRootErrorMessage(e);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
facesContext.addMessage(null, m);
}
}
,並在newItem.xhtml
<f:metadata>
<f:viewParam name="customerId"
value="#{newItemController.customerId}" />
<f:viewAction action="#{newItemController.loadData()}"/>
</f:metadata>
而現在它的工作原理...: )但現在我有一個新的問題..我會爲此創建一個單獨的問題:) 感謝您的幫助
'customerDas'沒有了'newItemController.java'內聲明,猜測而miniizing的是迷路了代碼示例? –
雅我刪除了很多最小化: @Inject private CustomerService customerDas; 這是簡單的數據訪問服務 – klind
酷,很高興看到它正在工作。您能否將答案標記爲「已接受」,以便其他成員和系統能夠正確識別?祝你有美好的一天...... –