1

I'm trying to understand the behavior of JSP and Managed bean interaction. (using JSF 1.2 to test this)
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.

The issues I faced were :

  1. For every Item in the cart page, I wanted a common remove method 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 backingBean.removeFromCart method.

    But that wasn't possible. As it couldn't interpret the value

    action="#{cartBackingBean.removeFromCart(#{cart.item.id})}"
    
  2. I thought may be I can use a InputHidden tag and pass the itemId to my remove function on button click.

    But even this never worked. When I get the cart object, it turned out to be null in jsp.

    <%CartData cart = (CartData) request.getAttribute("cart"); if(cart != null) out.println("<input type=\"Hidden\" id =\"itemId\" name=\"itemId\" value =\""+cart.getItem().getId() +"\" />");%>
    
  3. I changed the code to a href and passed the itemId in the request and using this in the code.
    This works, 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.

Over all code:

<f:view>
    <h:form id="search">
            <h:panelGrid bgcolor="skyblue" border="3">

                    <c:if test="${not empty cartBackingBean.cartData}">
                        <c:forEach var="cart" items="#{cartBackingBean.cartData}">

                            <h:panelGroup rendered="#{!cartBackingBean.invalidAccess}">
                                <h:graphicImage value="#{cart.item.imageUrl}" height="60" width="300"/>
                            </h:panelGroup>

                            <h:panelGroup rendered="#{!cartBackingBean.invalidAccess}">
                                <h:outputText value="#{cart.item.name}">
                                </h:outputText>
                            </h:panelGroup>

                            <h:panelGroup rendered="#{!cartBackingBean.invalidAccess}">
                                <a href="RemoveItemFromCart.view?itemId=${cart.item.id}" style="color: blue; font-style: italic;border: green; ">Remove item from cart</a>
                            </h:panelGroup>

                                <!--  Didn't Work -->
                                <%
                                    CartData cart = (CartData) request.getAttribute("cart");
                                    if(cart != null)
                                        out.println("<input type=\"Hidden\" id =\"itemId\" name =\"itemId\" value =\""+ cart.getItem().getId() +"\" />");
                                %>
                                <!--        <h:commandButton value="Remove from Cart" id="submit2" action="#{cartBackingBean.removeFromCart}" />  -->

                        </c:forEach> 
                    </c:if>

                        <h:panelGroup style="display:block; text-align:center" rendered="#{!cartBackingBean.invalidAccess}">
                            <div class="search">
                                <h:commandButton value="Add to Order" id="submit1" action="#{cartBackingBean.addToOrder}"/>
                                <h:commandButton image="images/submit.gif" value="Save Cart" id="submit" action="#{cartBackingBean.saveCart}"/>
                            </div>
                        </h:panelGroup>

                </h:panelGrid>
    </h:form>
</f:view>

Backing Bean code:

You can refer to this line // This is unnecessary code which I could've avoided if I can pass the exact cart object from UI. which I'm trying to avoid.

public String removeFromCart()
{
    Long itemId = -1L;

    if((Connection.getRequest().getParameter("itemId") != null 
            && Long.parseLong(Connection.getRequest().getParameter("itemId").toString()) >= 0))
    {
        itemId = Long.parseLong(Connection.getRequest().getParameter("itemId").toString());
    }

    if(itemId < 0L && (Connection.getRequest().getAttribute("itemId") != null 
            && Long.parseLong(Connection.getRequest().getAttribute("itemId").toString()) >= 0))
    {
        itemId = Long.parseLong(Connection.getRequest().getAttribute("itemId").toString());
    }

    if(itemId < 0L && (Connection.getRequest().getSession().getAttribute("itemId") != null 
            && Long.parseLong(Connection.getRequest().getSession().getAttribute("itemId").toString()) >= 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;
}

The complete code is available over here for review.

Also, I heard people saying not to use JSF tags in JSP as it wouldn't work or support.
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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52

0 Answers0