Friday 15 June 2012

java - JSF commandButton not call action -



java - JSF commandButton not call action -

i have jsf form render different inputtext , commandbutton if viewparam mode not null. without mode param, first button phone call function when click mode param, button not phone call function. it's submit , create postback. want phone call function, validate , redirect back.

<h:form style="border-top: solid 1px #dbdbdb"> <div class="form-group" style="width: 112px; float: left"> quantity:&nbsp;<h:messages class="messages"/> <h:inputtext value="#{ordercreatebean.stringquantity}" styleclass="form-control" style="margin-bottom: 10px" rendered="#{productbean.mode == null}" /> <h:commandbutton value="add 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 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 , 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 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 , 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"); (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); }

the signature of methods 'addselectedproduct' , 'addproduct' wrong. oracle docs says look allowed within value attribute of h:commandbutton tag:

'the look must evaluate public method takes no parameters, , returns object (the tostring() of called derive logical outcome) passed navigationhandler application.'

so way it:

xhtml code:

<h:commandbutton value="add 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 pid value string pid = facescontext.getcurrentinstance().getexternalcontext().getrequestparametermap().get("pid"); // convert pid integer value , stuff here homecoming null; }

and same holds first method.

java jsf-2

No comments:

Post a Comment