Include JSTL tag library in your JSP Page
1 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> |
Use JSTL to call the required URL path through Spring bindings
1 | <c:url var="findProductForCat" value="/products_ajax.do" /> |
HTML and EL code to enter in JSP page.
1 2 3 4 5 6 7 8 9 10 | <select class="select-width" id="category_list_d"> <option value="">Select Category</option> <c:forEach var="catList" items="${category_lst}"> <option value="${catList.categoryId}">${catList.categoryName }</option> </c:forEach> </select> <select class="select-width" id="product_list_d"> <option value="">Select Product</option> </select> |
Get data to JSP page after triggering first action using jQuery Ajax
(In this example after Selecting First Drop Down)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | $().ready( function() { $('#category_list_d').change( function() { $.getJSON('${findProductForCat}', { categoryId : $(this).val(), ajax : 'true' }, function(data) { var html = '<option value="">Select Product</option>'; var len = data.length; for ( var i = 0; i < len; i++) { html += '<option value="' + data[i].productId + '">' + data[i].productName + '</option>'; } html += '</option>'; $('#product_list_d').html(html); }); }); }); |
Spring Controller Definition when called the mapped URL though JSTL
1 2 3 4 5 6 7 8 9 10 11 12 | @RequestMapping(value = "/products_ajax", method = RequestMethod.GET) public @ResponseBody List<Product> productsForCategory( @RequestParam(value = "categoryId", required = true) Integer categoryId) throws IllegalStateException, SystemException { //Change this as per you requirement to generate values according to business logics ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BLProduct objProduct = (BLProduct) context.getBean("bl_product"); //Specify the returning object you want here return objProduct.listProductNameByCategoryId(categoryId); } |