如果jsf form
模式不为null,我有commandButton
呈现不同的inputText和viewParam
。没有模式参数,点击时第一个按钮调用功能,但有模式参数,按钮不调用功能。它总是提交并回复。我希望调用函数,验证并重定向回来。
<h:form style="border-top: solid 1px #DBDBDB">
<div class="form-group" style="width: 112px; float: left">
Quantity: <h:messages class="messages"/>
<h:inputText value="#{orderCreateBean.stringQuantity}" styleClass="form-control" style="margin-bottom: 10px" rendered="#{productBean.mode == null}" />
<h:commandButton value="Add to Order" action="#{orderCreateBean.addProduct(productBean.pid)}" styleClass="btn btn-primary" rendered="#{productBean.mode == null}"/>
<h:inputText value="#{orderEditBean.stringQuantity}" styleClass="form-control" style="margin-bottom: 10px" rendered="#{productBean.mode != null}" />
<h:commandButton value="Add to Edit" action="#{orderEditBean.addSelectedProduct(productBean.pid)}" styleClass="btn btn-primary" rendered="#{productBean.mode != null}" />
</div>
</h:form>
OrderEditBean.java
public void addSelectedProduct(int pid) {
boolean valid = false;
if (session.get("edit_products") != null && session.get("edit_number") != null) {
String edit_number = session.get("edit_number").toString();
List<OrderProductDetails> opds = (List<OrderProductDetails>) session.get("edit_products");
if (ApplicationHelper.isInteger(stringQuantity)) {
quantity = Integer.parseInt(stringQuantity);
if (0 < quantity && quantity <= 10) {
valid = true;
}
}
if (!valid) {
ApplicationHelper.addMessage("Quantity between 1 and 10");
} else {
OrderProductDetails opd = new OrderProductDetails();
opd.setProductId(new Products(pid));
opd.setQuantity(quantity);
opds.add(opd);
session.put("edit_products", opds);
ApplicationHelper.addMessage("Product added!");
}
ApplicationHelper.redirect("client/order/edit_products.xhtml?number=" + edit_number, true);
} else {
ApplicationHelper.addMessage("You are not in update mode!");
ApplicationHelper.redirect("/client/product/show.xhtml?pid=" + pid, true);
}
}
OrderCreateBean.java
public void addProduct(int pid) {
boolean valid = false;
if (ApplicationHelper.isInteger(stringQuantity)) {
quantity = Integer.parseInt(stringQuantity);
if (0 < quantity && quantity <= 10) {
valid = true;
}
}
if (!valid) {
ApplicationHelper.addMessage("Quantity between 1 and 10");
ApplicationHelper.redirect("/client/product/show.xhtml?pid=" + pid, true);
return;
}
session = SessionHelper.getSessionMap();
if (session.get("order_product_details") == null) {
List<OrderProductDetails> opds = new ArrayList<>();
OrderProductDetails opd = new OrderProductDetails();
opd.setProductId(new Products(pid));
opd.setQuantity(quantity);
opds.add(opd);
session.put("order_product_details", opds);
} else {
boolean exists = false;
List<OrderProductDetails> opds = (List<OrderProductDetails>) session.get("order_product_details");
for (OrderProductDetails opd : opds) {
if (opd.getProductId().getPid() == pid) {
opd.setQuantity(opd.getQuantity() + quantity);
exists = true;
break;
}
}
if (!exists) {
OrderProductDetails opd = new OrderProductDetails();
opd.setProductId(new Products(pid));
opd.setQuantity(quantity);
opds.add(opd);
}
session.put("order_product_details", opds);
}
ApplicationHelper.addMessage("Product added!");
ApplicationHelper.redirect("/client/order/selected_products.xhtml", true);
}
答案 0 :(得分:0)
您方法的签名&quot; addSelectedProduct&#39;和&#39; addProduct&#39;是错的。 这就是Oracle文档所说的关于h:commandButton标记的value属性中允许的表达式的内容:
&#39;表达式必须求值为不带参数的公共方法,并返回一个Object(调用toString()以获取逻辑结果),该对象将传递给此应用程序的NavigationHandler。 #39;
所以这就是我的方式:
XHTML代码:
<h:commandButton value="Add to Edit" action="{orderEditBean.addSelectedProduct}" styleClass="btn btn-primary" rendered="#{productBean.mode != null}">
<f:param name="pid" value="#{productBean.pid}" />
</h:commandButton>
MANAGEDBEAN CODE:
public String addProduct() {
// retrieve the pid value
String pid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pid");
// convert pid to integer value and do all your stuff here
return null;
}
并且第一种方法也是如此。