facebook

Struts Iterate Confusion

  1. MyEclipse IDE
  2.  > 
  3. Comments
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #215151 Reply

    Jason Price
    Member

    Hi

    Is it just me or is the struts documentation particularly unintuitive?

    I have a class (Invoice) which has a method that returns a Vector of relevant items (of type OrderItem) Item. I can’t get this to work with the struts iterate tag. This is the code I use on the jsp

    <logic:iterate id="item" name="invoice" property="orderItems" type="com.novatech.dal.OrderItem">
       <bean:write name="item" property="stockCode" />
    </logic:iterate>

    StockCode is a property of OrderItem class. I just get “cannot find bean item in any scope” errors. I know the classes work as if I do the in the same page it works fine.

    <jsp:useBean id="invoice" type="com.novatech.dal.Invoice" scope="session" />
    <%
    Vector items = invoice.getOrderItems();
    for(int a =0;a<items.size();a++) {
    OrderItem item = (OrderItem)items.get(a);
    //print out order items for this invoice
    out.println("<BR> " + item.getStockCode()    + "," + item.getDescription() + "," + item.getQty() + ","+ item.getTotal() + "," + item.getId() + "," + item.getOrdDate());
    }
    %>

    Can anyone help?

    #215156 Reply

    bort
    Member

    Can I assume that your class structure looks something like this?

    
    public class Invoice {
        private Vector orderItems;
    
        public Vector getOrderItems() {
            return orderItems;
        }
    
        public void setOrderItems(Vector v) {
            orderItems = v;
        }
    }
    

    Are you adding your object of type Invoice to the request/session?

    
    Invoice inv = new Invoice();
    request.setAttribute("invoice", inv);
    

    Try this:

    
    <bean:define id="orderItemsVector" name="invoice" property="orderItems"/>
    <logic:iterate id="item" name="orderItemsVector" type="com...OrderItem">
        // blah blah blah
    </logic:iterate>
    
    #215184 Reply

    Jason Price
    Member

    That worked a treat, thankyou.

    I am confused as to why I had to define the bean as none of the examples I have seen mention this at all?

    On a slightly different note in my action class that creates the invoice I have tried to setAttribute on the request scope but I can’t see it in the jsp page (bean not in any scope again), so I am using the session at the moment.

    Is this because the action class passes this to the struts controller servlet, which in turn does not pass this on to the jsp page?

    I thought that if I place something in a servletts request scope it would be available to the forwarded jsp page as well?

    #215187 Reply

    bort
    Member

    @FunkyHippy wrote:

    I am confused as to why I had to define the bean as none of the examples I have seen mention this at all?

    The “invoice” was readily accessible to you, but the Vector within it, wasn’t. You needed to reach into the “invoice” object and define the Vector for your session.

    @FunkyHippy wrote:

    On a slightly different note in my action class that creates the invoice I have tried to setAttribute on the request scope but I can’t see it in the jsp page (bean not in any scope again), so I am using the session at the moment.

    Is this because the action class passes this to the struts controller servlet, which in turn does not pass this on to the jsp page?

    I thought that if I place something in a servletts request scope it would be available to the forwarded jsp page as well?

    Your thought is correct. Can you provide some snippets of your Action method, the JSP, and the appropriate action-mapping in your struts-config.xml?

    #215429 Reply

    Jason Price
    Member

    I need to keep this bean in the session now as I need to access it across several pages but I think the following is related to the same problem:

    I am trying to use actionmessages on my pages to report various outcomes (that aren’t errors). Here is what I have:

    in my ApplicationResources.properties file:

    #messages
    message.invoice.itemScanned={0} was removed from the list.
    message.invoice.itemNotScanned={0} was not a valid item.

    in my struts-config.xml:

    <action
             attribute="dispatchInvoiceForm"
             input="/form/dispatchInvoice.jsp"
             name="dispatchInvoiceForm"
             path="/dispatchInvoice"
             scope="request"
             type="com.novatech.struts.action.DispatchInvoiceAction">
             <forward
                name="scanning"
                path="/dispatch/dispatchInvoice.jsp"
                redirect="true" />
          </action>
    

    in action class (where found is a boolean) :

    if(found) {
       ActionMessage msg = new ActionMessage("message.invoice.itemScanned",stockCode);
      messages.add("Message1", msg);
    } else {
       ActionMessage msg = new ActionMessage("message.invoice.itemNotScanned",stockCode);
       messages.add("Message1", msg);
    }
            
    //make messages availbe to jsp page
    saveMessages(request, messages);

    and in my jsp page:

      <html:messages id="msg" message="true">
    <bean:write name="msg"/><br>
    </html:messages>

    everything works as it should but I get no messages displayed in my page, so I am assuming that there is nothing in the request context.

    #215441 Reply

    Scott Anderson
    Participant

    everything works as it should but I get no messages displayed in my page, so I am assuming that there is nothing in the request context.

    You should be able to verify this by setting a breakpoint on one of the Java lines in your JSP and looking at the context in the debugger’s Variables view.

    #215828 Reply

    Jason Price
    Member

    I’ve checked and there its definately not in the request context.
    any ideas?

    #215842 Reply

    Riyad Kalla
    Member

    Funky, this is part of the learning curve with Struts… always pay attention to your use of redirects and such, a redirect will cause your request variables to go bye-bye before you get to your page because it introduces one more step in the client->server communication where the request is cleared before or after the dispatch to the new page is done. However in 90% of the cases, NOT redirecting is simply not an option because you don’t want the user hitting refresh or something of that nature and reexecuting that action all over again (like logging in).

    This is 1) confusing and 2) annoying as hell as you end up sticking so much stuff in the session that you don’t really need there.

    #215884 Reply

    Jason Price
    Member

    really? that is annoying!
    Actually I don’t need to redirect for these messages as it posts back to the same page but I can see how much of a pain thats going to be for other actions.
    Guess I’ll just have to get used to removing stuff out of the session all the time!

    I would never have figured that out on my own, thanks very much.

    #215890 Reply

    Riyad Kalla
    Member

    No problem, and yes I completely agree that its annoying. I think Struts should have supported a sort of ‘virtual’ request scope that can handle up to 1 dispatch (redirect=true) and still maintain the request scoped variables… but since it can’t, everything goes into the session in my apps because I redirect on almost every single page.

    I wonder how WebWork2 handles this? People seem to like that framework + Spring + Hibernate quite a bit (the Atlassian team swears by it).

Viewing 10 posts - 1 through 10 (of 10 total)
Reply To: Struts Iterate Confusion

You must be logged in to post in the forum log in