| Author |
Struts Validator Plug-In
|
Raghuveer Rawat
Ranch Hand
Joined: Apr 03, 2003
Posts: 102
|
|
I am using struts validator framework for validation. I have defined my validation in validation.xml file like this. <form name="itemCheckInForm"> <field property="quantity" depends="required,float"> <arg0 key="prompt.quantity"/> </field> <field property="receiveDate" depends="required,date"> <arg0 key="prompt.receiveDate"/> <var> <var-name>datePatternStrict</var-name> <var-value>MM-dd-yyyy</var-value> </var> </field> </form> in the ActionForm Bean which is extending ValidatorForm I am overriding validate method like this: public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // Perform validator framework validations ActionErrors errors = super.validate(mapping,request); // Only need crossfield validations here if (quantity == null || quantity.trim().length() < 1 ) { errors.add("quantity", new ActionError("modelNumber.required")); } if (receiveDate == null || receiveDate.trim().length() < 1 ) { errors.add("receiveDate", new ActionError("receiveDate.required")); } return errors; } My Action Class is: public class ItemCheckInAction extends Action { /** Creates a new instance of ItemCheckInAction */ public ItemCheckInAction() { } private Log log = LogFactory.getLog("com.delhicall.ErrorLog"); public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception { try { Locale locale = getLocale(request); MessageResources messages = getResources(request); ActionErrors errors = new ActionErrors(); HttpSession session = request.getSession(); ItemCheckInForm newitemform = (ItemCheckInForm) form; ItemTransactionBean itemBean = new ItemTransactionBean(); AdminModuleBean adminBean = new AdminModuleBean(); String categoryList =null; String retmsg =null; String employeeId =""; String action = newitemform.getAction(); String category = newitemform.getCategory(); String itemCode = newitemform.getItem(); String parentItem = newitemform.getParentItem(); String vendorCode = newitemform.getVendorCode(); String quantity = newitemform.getQuantity(); // String uomId = newitemform.getUomList(); String receiveDate = newitemform.getReceiveDate(); String roomCode = newitemform.getRoomCode(); String siteCode = newitemform.getSiteCode(); String maintenanceCost=newitemform.getMaintenanceCost(); String maintenanceCostUom =newitemform.getCurrencyCode(); String comment =newitemform.getComment(); Collection categories = adminBean.getCategories(); request.setAttribute("categories", categories); Collection currencies = adminBean.getCurrencies(); request.setAttribute("currencies", currencies); // Collection uoms = adminBean.getUoms(); // request.setAttribute("uoms", uoms); Collection sites = adminBean.getSites(); request.setAttribute("sites", sites); if(siteCode==null || siteCode.equals("")) { System.out.println("inside if"); Iterator site = sites.iterator(); if(site.hasNext()) { System.out.println("insite inner if"); ArrayList list = new ArrayList(); //list =(ArrayList) site.next(); SiteBean siteBean = (SiteBean)site.next(); System.out.println("one"); // SiteBean siteBean = (SiteBean)list.get(0); // System.out.println("two"); siteCode = Integer.toString(siteBean.getId()); System.out.println("siteCode: "+siteCode); } } Collection rooms = adminBean.getLocations(siteCode); request.setAttribute("rooms", rooms); if(category==null || category.equals("")) { Iterator cat = categories.iterator(); if(cat.hasNext()) { ArrayList list = new ArrayList(); CategoryBean catBean = (CategoryBean) cat.next(); // CategoryBean catBean = (CategoryBean)list.get(0); category = Integer.toString(catBean.getId()); System.out.println("category: "+category); } } Collection items = adminBean.getItems(category); request.setAttribute("items", items); Collection vendors = adminBean.getVendors(); request.setAttribute("vendors", vendors); if(action!=null && action.equals("save")) { if (log.isTraceEnabled()) { log.trace("Performing extra validations"); } if (quantity == null || quantity.trim().length() < 1 ) { errors.add("quantity", new ActionError("modelNumber.required")); } if (receiveDate == null || receiveDate.trim().length() < 1 ) { errors.add("receiveDate", new ActionError("receiveDate.required")); } if (!errors.isEmpty()) { saveErrors(request, errors); System.out.println("Inside Save Error"); saveToken(request); return (mapping.findForward("error")); // return (mapping.getInputForward()); } //String errorMsg=itemBean.saveNewItem(itemCode,parentItem,employeeId,statusCode,serialNumber,warrentyTime,invoiceNumber,invoiceDate,vendorCode,receiveDate,pricePerUnit,purchasePrice,barCode,quantity,roomCode); String errorMsg=itemBean.saveItemCheckIn(itemCode,parentItem,employeeId,vendorCode,receiveDate,quantity,roomCode,maintenanceCost,maintenanceCostUom,comment); System.out.println("errorMsg: "+errorMsg); if (errorMsg !=null && errorMsg.equals("Duplicate")) { // errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("item.unique")); // return (mapping.findForward("error")); } if (!errors.isEmpty()) { saveErrors(request, errors); System.out.println("Inside Save Error"); saveToken(request); return (mapping.findForward("error")); // return (mapping.getInputForward()); } } newitemform.setAction(action); newitemform.setCategory(category); newitemform.setItem(itemCode); newitemform.setParentItem(parentItem); newitemform.setVendorCode(vendorCode); newitemform.setQuantity(quantity); // newitemform.setUomList(uomId); newitemform.setReceiveDate(receiveDate); newitemform.setRoomCode(roomCode); newitemform.setSiteCode(siteCode); newitemform.setMaintenanceCost(maintenanceCost); newitemform.setCurrencyCode(maintenanceCostUom); request.setAttribute(mapping.getAttribute(), newitemform); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t == null) { t = e; } log.error("Item Entry Failed", t); throw new ServletException("Item Entry Failed", t); } catch (Throwable t) { log.error("Item Entry Failded", t); throw new ServletException("Item Entry Failed", t); } // Forward control to the specified success URI if (log.isTraceEnabled()) { log.trace("Forwarding to Entry page"); } return (mapping.findForward("success")); } } my problem is when I submit my jsp page and I enter incorrect value I get a dialog box stating that I have entered wrong value. But form also get submited I mean my save method is called in Action Class. I think when form is not validate I should not have access to the Action Class. Can anyone who have used validator Plug-In will check what is the problem in this code. Thanks
|
Raghuveer Rawat<br />SCJP2
|
 |
 |
|
|
subject: Struts Validator Plug-In
|
|
|