我正在創建一個按鈕或鏈接,以便在用戶搜索產品後將產品添加到購物車。以下是我們的ProdcutsController.java類的部分:使用commandButton與字符串動態按鈕的數據表
public String getResponse() {
String resultStr = "";
resultStr += theModel.getProductID() + "<br/>"
+ theModel.getUserID() + "<br/>"
+ theModel.getTitle() + "<br/>"
+ theModel.getProductType() + "<br/>"
+ theModel.getProductDesc() + "<br/>"
+ theModel.getRating() + "<br/>"
+ theModel.getPictureURL() + "<br/>"
+ theModel.getPrice() + "<br/>";
response = resultStr;
return response;
}
public String searchProduct() {
ProductsDAO aProductDAO = new ProductsDAOImpl(); // Creating a new object each time.
ArrayList products = aProductDAO.searchProduct(theModel);
response = "";
for (int i=0; i < products.size(); i++) {
theModel = (ProductsBean)products.get(i);
response += getResponse();
}
theModel.setSearchResult(response);
return "searchResults.xhtml"; // navigate to "searchResults.xhtml"
}
現在它顯示爲看起來很糟糕。我對JSF非常陌生(在我們的課程中使用2.1版本),並且之前曾使用ASP.NET進行類似的課程。我原來的想法,我認爲違反了BalusC's comment for "h:commandLink/h:commandButton is not being invoked"中的規則#2,是在響應+ = getResponse()後添加一個調用我們的方法的命令按鈕的JSF代碼,在產品控制器中的addToCart()。它顯示相應搜索結果旁邊的按鈕,但不運行#{cartController.addToCart()}方法。
我看着數據表,如BalusC's comment for "JSF Command button inside a JSF data Table"。基本上可以歸結爲兩個問題:
- 我們應該嘗試使用數據表來顯示我們的數據並在列中使用「添加到購物車」按鈕嗎?我可以將此綁定到該特定產品ID的addToCart()方法嗎?我問「應該」,因爲我們是JSF的新手,這可能會在我們頭上。
- 我的第一個解決方案是否可行?我們可以動態地在一個字符串中動態創建可操作的JSF表單按鈕嗎?
- 有沒有比我們試圖更簡單的東西?
任何幫助,非常感謝。我們目前正在使用JSF 2.1。