我不知道什麼方法以及如何在開始編碼時獲取特定對象項目的id
,當單擊表單按鈕時。在列表中獲取項目的id在JSF中
的產品編號值表示是主鍵我創建產品表的。我來自Swing編程,並在Swing我可以很容易地使用JLabel.getText();
來獲取屏幕截圖上顯示的產品Idds 1,4,5,6
。但我想這與JSF不一樣。
而且,這些對象也包含在ui:repeat
循環
frames.xhtml
<ui:repeat value="#{frameBean.all}" var="f" varStatus="loop">
<h:outputText escape="false" rendered="#{loop.index % 3 == 0}" />
<div >
<div class="col-sm-6 col-md-3">
<div class="thumbnail clearfix">
<img src="..." alt="frame image placeholder" width="240" height="180"/>
<div class="caption">
<p>
<h:outputLabel>Product ID: </h:outputLabel>
<h:outputText value="#{f.product_id}"/>
</p>
<p><h:outputLabel value="#{f.name}"/></p>
<p><h:outputText value="#{f.description}"/></p>
<p>
<h:panelGroup rendered="#{login.userRole eq 'customer' or login.userRole eq null}">
<a href="#" class="btn btn-primary pull-right" role="button">Add To Cart</a>
</h:panelGroup>
</p>
</div>
</div>
</div>
</div>
<h:outputText escape="false" rendered="#{loop.last or (loop.index + 1) % 3 == 0}" />
</ui:repeat>
當然,我可以使用f.product_Id
,但我怎樣才能得到一個特定的ID,如果添加到購物車按鈕被點擊?
下面是一個包含了getAll()
幀方法
public List<Frame> getAll() throws SQLException {
List<Frame> list = new ArrayList<>();
PreparedStatement ps = null;
try {
String SQL = "select product_id, name, description, price_per_sqft, material from product where isFrame = 1";
ps = con.prepareStatement(SQL);
//get customer data from database
ResultSet result = ps.executeQuery();
while (result.next()) {
Frame frame = new Frame();
frame.setProduct_id(result.getInt("product_id"));
frame.setName(result.getString("name"));
frame.setDescription(result.getString("description"));
frame.setPrice_per_sqft(result.getDouble("price_per_sqft"));
frame.setMaterial(result.getString("material"));
//store all data into a List
list.add(frame);
}
} catch (SQLException | HeadlessException e) {
e.printStackTrace();
} finally {
try {
con.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
爲了得到我想創造一個addToCart(int aProductId)
方法的框架的產品ID的目的的實現。這將需要aProductId
參數。
謝謝。在bean
<p>
<h:outputLabel>Product ID: </h:outputLabel>
<h:outputText value="#{f.product_id}"/>
<f:param name="productId" value=""#{f.product_id}""></f:param>
</p>
後來PARAM組件店面的產品ID,您可以通過使用FacesContext的
String propertyId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
.get("productId");
Offtopic:不要在getters中做任何工作:http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – Kukeltje