Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop through a list in jsp and pass the corresponding object's id on click
    primarykey
    data
    text
    <p>I'm trying to understand the <strong>behavior of JSP and Managed bean interaction</strong>. (using JSF 1.2 to test this)<br/> I have a cart page which displays a list of items that were added to the cart and a remove button at the end of each row which enables user to remove that particular item from the cart.</p> <p>The <strong>issues I faced</strong> were :</p> <ol> <li><p>For every Item in the cart page, <strong>I wanted a common remove method</strong> which gets invoked for that object when the remove item button is clicked. I thought of passing the button's corresponding data object's id value to the <code>backingBean.removeFromCart</code> method.</p> <p>But that wasn't possible. As it couldn't interpret the value</p> <pre><code>action="#{cartBackingBean.removeFromCart(#{cart.item.id})}" </code></pre></li> <li><p>I thought may be I can use a <code>InputHidden</code> tag and pass the itemId to my remove function on button click.</p> <p>But even this never worked. When I get the <strong>cart object, it turned out to be null</strong> in jsp.</p> <pre><code>&lt;%CartData cart = (CartData) request.getAttribute("cart"); if(cart != null) out.println("&lt;input type=\"Hidden\" id =\"itemId\" name=\"itemId\" value =\""+cart.getItem().getId() +"\" /&gt;");%&gt; </code></pre></li> <li><p>I changed the code to a <code>href</code> and passed the itemId in the request and using this in the code.<br/> <strong>This works</strong>, however it is a little annoying because I know the object in the cart that needs to be deleted. So instead of using that reference I need to pass the itemId and it sounds more like a hack.</p></li> </ol> <p><strong>Over all code:</strong></p> <pre><code>&lt;f:view&gt; &lt;h:form id="search"&gt; &lt;h:panelGrid bgcolor="skyblue" border="3"&gt; &lt;c:if test="${not empty cartBackingBean.cartData}"&gt; &lt;c:forEach var="cart" items="#{cartBackingBean.cartData}"&gt; &lt;h:panelGroup rendered="#{!cartBackingBean.invalidAccess}"&gt; &lt;h:graphicImage value="#{cart.item.imageUrl}" height="60" width="300"/&gt; &lt;/h:panelGroup&gt; &lt;h:panelGroup rendered="#{!cartBackingBean.invalidAccess}"&gt; &lt;h:outputText value="#{cart.item.name}"&gt; &lt;/h:outputText&gt; &lt;/h:panelGroup&gt; &lt;h:panelGroup rendered="#{!cartBackingBean.invalidAccess}"&gt; &lt;a href="RemoveItemFromCart.view?itemId=${cart.item.id}" style="color: blue; font-style: italic;border: green; "&gt;Remove item from cart&lt;/a&gt; &lt;/h:panelGroup&gt; &lt;!-- Didn't Work --&gt; &lt;% CartData cart = (CartData) request.getAttribute("cart"); if(cart != null) out.println("&lt;input type=\"Hidden\" id =\"itemId\" name =\"itemId\" value =\""+ cart.getItem().getId() +"\" /&gt;"); %&gt; &lt;!-- &lt;h:commandButton value="Remove from Cart" id="submit2" action="#{cartBackingBean.removeFromCart}" /&gt; --&gt; &lt;/c:forEach&gt; &lt;/c:if&gt; &lt;h:panelGroup style="display:block; text-align:center" rendered="#{!cartBackingBean.invalidAccess}"&gt; &lt;div class="search"&gt; &lt;h:commandButton value="Add to Order" id="submit1" action="#{cartBackingBean.addToOrder}"/&gt; &lt;h:commandButton image="images/submit.gif" value="Save Cart" id="submit" action="#{cartBackingBean.saveCart}"/&gt; &lt;/div&gt; &lt;/h:panelGroup&gt; &lt;/h:panelGrid&gt; &lt;/h:form&gt; &lt;/f:view&gt; </code></pre> <p><strong>Backing Bean</strong> code:</p> <p>You can refer to this line <code>// This is unnecessary code which I could've avoided if I can pass the exact cart object from UI.</code> which I'm trying to avoid.</p> <pre><code>public String removeFromCart() { Long itemId = -1L; if((Connection.getRequest().getParameter("itemId") != null &amp;&amp; Long.parseLong(Connection.getRequest().getParameter("itemId").toString()) &gt;= 0)) { itemId = Long.parseLong(Connection.getRequest().getParameter("itemId").toString()); } if(itemId &lt; 0L &amp;&amp; (Connection.getRequest().getAttribute("itemId") != null &amp;&amp; Long.parseLong(Connection.getRequest().getAttribute("itemId").toString()) &gt;= 0)) { itemId = Long.parseLong(Connection.getRequest().getAttribute("itemId").toString()); } if(itemId &lt; 0L &amp;&amp; (Connection.getRequest().getSession().getAttribute("itemId") != null &amp;&amp; Long.parseLong(Connection.getRequest().getSession().getAttribute("itemId").toString()) &gt;= 0)) { itemId = Long.parseLong(Connection.getRequest().getSession().getAttribute("itemId").toString()); } this.clearError(); if(itemId == -1L) { this.setError("Could not delete as item was not found"); return Constants.ERROR; } // This is unnecessary code which I could've avoided if I can pass the exact cart object from UI. if(!Util.isNullList(getCartData())) { for(CartData cart : this.cartData) { if(cart.getItemId() == itemId) { cart.setItem(null); Connection.getCurrentDBSession().delete(cart); break; } } } this.setError(Constants.EMPTY_STRING); Util.removeBean("cartBackingBean"); Connection.getRequest().getSession().setAttribute(Constants.FORWARD_MESSAGE_TO_NEXT_BB, "Item removed from Cart Successfully!!"); return Constants.CART; } </code></pre> <p>The <strong>complete code</strong> is available over <a href="https://github.com/bandikishores/ShoppingCart/blob/master/src/backingbean/CartBackingBean.java" rel="nofollow noreferrer">here</a> for review.</p> <p>Also, I heard people saying not to use JSF tags in JSP as it wouldn't work or support.<br/> But I have been running this code using JSP and it works fine. Could someone please explain as to why should we not mix these technologies? and when should one use either of them?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload