While doing code re-factoring today on a major function. I discovered a super funny and interesting problem
To start with, take a look in the following code
It is very funny looking at the highlighted segment. If your page is targeted to saveProductBarcode.htm, then the function saveProductBarcode is invoked. For this reason, why you need to pass a parameter with a value “save” to invoke the logics?
The fact is, it is not the only function inside the controller writing in this approach. Nearly every function is like that......
Consider another piece of code segment,
This is web programming 101. When you wanna pass parameter to the server, you can use either GET/POST parameter to bring information back to the controller from the web user interface. The fact is, you are NOT LIMITED to use ONLY ONE parameter. You can use multiple of them. This is really the first time for me to see someone wrote (parameter.equals("delete_product_barcode_" + i).
When i dig a little deeper into it, the parameter is actually like “delete_product_barcode_0” or “delete_product_barcode_1” and so on..... What the developer has to do then, is to parse the string value of the parameter and get the ith value. Come on, you can actually use http://xxx/xxx/do_something.jsp?action=delete_product_barcode&item=i
I guess this is not something very difficult, isn’t it ?
Last but not least, you are returning to “sve/product/reference/productdetail/index” anyway, you can omit the one inside the if-condition when “removeProductBarcode != null”
To start with, take a look in the following code
| @RequestMapping(value="saveProductBarcode.htm") public ModelAndView saveProductBarcode(Model model, @ModelAttribute("productDetailForm") ProductDetailForm productDetailForm, @ModelAttribute("productDetail_action") String productDetail_action, HttpServletRequest request) throws Exception{ String parameter = request.getParameter("parameter"); if (parameter != null && parameter.equals("save")){ ProductBarcode productBarcode = new ProductBarcode(); productBarcode.setBarcode(productDetailForm.getBarcode()); productBarcode.setSysStatus(ProductBarcode.ACTIVE); productBarcode.setProduct(productDetailForm.getProduct()); productDetailService.saveProductBarcode(productBarcode); ... model.addAttribute("productDetailForm", productDetailForm); return new ModelAndView("/sve/product/reference/productdetail/index"); } return new ModelAndView("/sve/product/reference/productdetail/index"); } |
It is very funny looking at the highlighted segment. If your page is targeted to saveProductBarcode.htm, then the function saveProductBarcode is invoked. For this reason, why you need to pass a parameter with a value “save” to invoke the logics?
The fact is, it is not the only function inside the controller writing in this approach. Nearly every function is like that......
Consider another piece of code segment,
| @RequestMapping(value="removeProductBarcode.htm") public ModelAndView removeProductBarcode(Model model, @ModelAttribute("productDetailForm") ProductDetailForm productDetailForm, @ModelAttribute("productDetail_action") String productDetail_action, HttpServletRequest request) throws Exception{ String parameter = request.getParameter("parameter"); ProductBarcode removeProductBarcode = null; for (ProductBarcode productBarcode :productDetailForm.getProduct().getProductBarcodes()){ int i = ((List .getProduct().getProductBarcodes()).indexOf(productBarcode); // Remove Product Barcode if (parameter.equals("delete_product_barcode_" + i)){ removeProductBarcode = productBarcode; break; } } if (removeProductBarcode != null){ productDetailForm.getProduct(). getProductBarcodes().remove(removeProductBarcode); productDetailService.deleteProductBarcode(removeProductBarcode); return new ModelAndView("/sve/product/reference/productdetail/index"); } return new ModelAndView("/sve/product/reference/productdetail/index"); } |
This is web programming 101. When you wanna pass parameter to the server, you can use either GET/POST parameter to bring information back to the controller from the web user interface. The fact is, you are NOT LIMITED to use ONLY ONE parameter. You can use multiple of them. This is really the first time for me to see someone wrote (parameter.equals("delete_product_barcode_" + i).
When i dig a little deeper into it, the parameter is actually like “delete_product_barcode_0” or “delete_product_barcode_1” and so on..... What the developer has to do then, is to parse the string value of the parameter and get the ith value. Come on, you can actually use http://xxx/xxx/do_something.jsp?action=delete_product_barcode&item=i
I guess this is not something very difficult, isn’t it ?
Last but not least, you are returning to “sve/product/reference/productdetail/index” anyway, you can omit the one inside the if-condition when “removeProductBarcode != null”
