Results 1 to 6 of 6

Thread: Need help for Data Binding!!

  1. #1
    Join Date
    Feb 2010
    Location
    Bangalore,India
    Posts
    18

    Default Need help for Data Binding!!

    Hi All,

    Iam using Spring MVC 3 and jstl for jsp's.

    My problem is iam returning a ArrayList<Some Class> to jsp which will iterate the list through c:forEach tag.So,in the view page there is one text box(like quantity) where user can input some data which is a variable in (Some Class).
    When submitting the form i am not getting the user updated value in controller class.



    Any solutions for this scenario(I can do this using javascript and hidden variable,but is there a way in spring where it autopopulates the user entered value in list and return back to controller).

    Rgds
    Rana

  2. #2
    Join Date
    May 2011
    Location
    Hamburg Germany
    Posts
    33

    Default

    Hi Rana,

    this is a very very common scenario and lots of people do it every day. Your ArrayList needs to be part of your form backing object. If you'd like some specific help on your code I'd suggest posting some.

    happy coding

    Andreas

  3. #3
    Join Date
    Feb 2010
    Location
    Bangalore,India
    Posts
    18

    Default

    Hi,

    Thanks for the reply.

    Actully iam using spring mvc annotated controller .Please see below code

    Controller Method where iam returning list(productList)
    Code:
    	@RequestMapping(value="/order/getEcpOrderCreateDetailPage.htm",method=RequestMethod.GET)
    	public ModelAndView getDetailPage(HttpServletRequest req,
    			HttpServletResponse res,
    			@ModelAttribute PFGOrderCreateHeader orderCreateHeader)
    			throws ExceptionMessage {
    		List<Product> productsList=getProductList Here
    		modelAndView.addObject("productList",productsList);
    		return modelAndView;
    	}
    Jsp Page Iteration[Note that there is one text box where user can input]
    Code:
    			<c:forEach items="${productList}" var="productsList">
    				<TR>
    					<!--Table row n -->
    					<TD ALIGN="RIGHT" NOWRAP>
    						${productsList.upc}
    					</TD>
    					<TD ALIGN="RIGHT" NOWRAP>
    						${productsList.productCode}
    					</TD>
    					<spring:bind path="productList.tbProdQty">
    					<TD ALIGN="RIGHT" NOWRAP>
    						<input type="text" SIZE="5" MAXLENGTH="5" name="tbProdQty" />
    					</TD>
    					</spring:bind>
    					<TD ALIGN="CENTER" NOWRAP>
    						Case
    					</TD>
    					<TD ALIGN="CENTER" NOWRAP>
    						${productsList.leadTime}
    					</TD>
    					<TD ALIGN="LEFT" NOWRAP>
    						${productsList.description}
    					</TD>
    					<TD ALIGN="CENTER" NOWRAP>
    						${productsList.explicit}
    					</TD>
    				</TR>
    				</c:forEach>
    When user click on Submit button ,below controlle class
    Code:
    	@RequestMapping(value="/order/orderCreate.htm",method=RequestMethod.POST)
    	public ModelAndView orderCreate(HttpServletRequest req,
    			HttpServletResponse res,@ModelAttribute("orderCreateHeader") PFGOrderCreateHeader orderCreateHeader,ArrayList<Product> pList) {
    		logger.info("productList:->"+pList);
    		for(Product product:pList){
    			logger.info("UPC:"+product.getUpc());
    			logger.info("Quantity:"+product.getTbProdQty());
    		}
    		return null;
    	}

  4. #4
    Join Date
    May 2011
    Location
    Hamburg Germany
    Posts
    33

    Default

    Ok,

    Code:
    			<c:forEach items="${productList}" var="product" varStatus="rowStatus" >
    				<TR>
    					<!--Table row n -->
    					<TD ALIGN="RIGHT" NOWRAP>
    						${product.upc}
    					</TD>
    
    					<!--Table row n .... -->
    
    					<spring:bind path="productList[${rowStatus.index}].tbProdQty" >
    					<TD ALIGN="RIGHT" NOWRAP>
    						<input type="text" SIZE="5" MAXLENGTH="5" name="${status.expression}" />
    					</TD>
    					</spring:bind>
    
    					<!--Table row n .... -->
    
    				</TR>
    				</c:forEach>
    or

    Code:
    			<c:forEach items="${productList}" var="product" varStatus="rowStatus" >
    				<TR>
    					<!--Table row n -->
    					<TD ALIGN="RIGHT" NOWRAP>
    						${product.upc}
    					</TD>
    
    					<!--Table row n .... -->
    
    					<form:input path="productList[${rowStatus.index}].tbProdQty" />
    
    					<!--Table row n .... -->
    
    				</TR>
    				</c:forEach>

    or, if you'd use shept.org (see below) you won't have to deal with the loop yourself - instead

    Code:
    	  <jsp:attribute name="tabRow" >
    		<tr class="${rowClassDefault}">
    			<td>
                               ${rowObject.upc}
    			</td>
    					
                            <!--Table row n -->
    
    			<td>
    			   <form:input path="tbProdQty" cssErrorClass="sheptFieldError" />
    			</td>
    The embedding <table:table /> sets the context path for each row via <spring:nestedPath /> so you just deal with the last accessor name.
    As an extra benefit you'll get sorting and paging your productList for free !

    happy coding

    Andreas

  5. #5
    Join Date
    Feb 2010
    Location
    Bangalore,India
    Posts
    18

    Default

    Hi,

    I am facing following issue when using code suggested by you.

    Caused by: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'productList[0]' available as request attribute
    at org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java:122)
    at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag.java:7 9)
    at jsp_servlet._web_45_inf._jsp._order.__pgecpordercr eatedetail._jspService(__pgecpordercreatedetail.ja va:170)
    at weblogic.servlet.jsp.JspBase.service(JspBase.java: 34)
    at weblogic.servlet.internal.StubSecurityHelper$Servl etServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invok eServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute( ServletStubImpl.java:300)
    at weblogic.servlet.internal.ServletStubImpl.execute( ServletStubImpl.java:183)
    at weblogic.servlet.internal.RequestDispatcherImpl.in vokeServlet(RequestDispatcherImpl.java:526)
    at weblogic.servlet.internal.RequestDispatcherImpl.in clude(RequestDispatcherImpl.java:447)
    at weblogic.servlet.jsp.PageContextImpl.include(PageC ontextImpl.java:163)
    at weblogic.servlet.jsp.PageContextImpl.include(PageC ontextImpl.java:184)
    at org.apache.tiles.jsp.context.JspTilesRequestContex t.include(JspTilesRequestContext.java:103)

    But actually i have set productList in request in controller as well..

    List<Product> productsList=getProductsForOrderCreate(orderCreate Header);
    modelAndView.addObject("orderCreateHeader",orderCr eateHeader);
    modelAndView.addObject("productList",productsList) ;

    I have tried setting productList expiclitly in request object as well as

    req.setAttribute("productList",productsList);

    Rgds
    Rana

  6. #6
    Join Date
    Feb 2010
    Location
    Bangalore,India
    Posts
    18

    Default

    Hi,

    Thanks for the code.
    I am able to set in list.

    Thanks a lot

    Rana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •