Iterating with struts

Posted on 2011-10-29
Last Modified: 2012-05-12

I have the following code:

<form method="POST" action="details.jsp?code=">
            <tr><td><input type="button" value="Back" onClick="history.go(-1);" class='Commandbutton'/></td><td><input type="submit" align='right' value="New" class='Commandbutton'/></tr>
      for(int ii=0; ii<code.length; ii++) {
            <tr><td><a href="details.jsp?code=<%=code[ii]%>&decode=<%=decode[ii]%>"><%=code[ii]%><a></td><td><%=decode[ii]%></td></tr>
            <tr><td><input type="button" value="Back" onClick="history.go(-1);" class='Commandbutton'/></td><td><input type="submit" align='right' value="New" class='Commandbutton'/></td></tr>

where I'm using a for loop index to pull values out of previously defined "code" and "decode" string arrays. How would I do this in struts? The only thing that I see if a foreach and iterate tag but it only iterates through one collection and does not allow indexing to multiple collections at once.  

Question by:tomsawyer213
    LVL 1

    Accepted Solution

    Seeing your above code it looks like the code and decode array have the same number of elements. Assuming that you can use the "indexId" attribute of the Struts "iterate" tag to achieve the same above logic.

    Something in the lines of -
    <logic:iterate name="code" id="element" indexId="index">
           <html:text property="code[index]" />
           <html:text property="decode[index]" />

    Hope this helps.

    Author Comment

    Ok I've got one more question for you. if I have the following code in my jsp

    <html:form method="POST" action="/details">
                        <td><html:text name="detailsActionForm" property="code"/></td></tr>
                <td><html:text name="detailsActionForm" property="decode"/></td></tr>
                <tr><td><html:submit value="Save"/></td><td><input type="reset" value="Reset"/></td></tr>

    and the following action in my struts-config.xml file which the jsp code points to

    <action input="/" name="detailsActionForm" path="/details" scope="session" type="" validate="false">
              <forward name="success" path="/listDetails.jsp?update=true&amp;oldcode="/>

    if you notice the URL on the success path is "listDetails.jsp?update=true&amp;oldcode=".

    How to i pass the value of the oldcode parameter from my jsp to this URL in struts-config.xml?



    Author Comment

    i was under the assumption i could use request.setAttribute("oldcode", listDetailsActionForm.getOldCode()); in my action class to set this uri parameter value.

    Author Comment

    Im assuming something like this in my Action class would suffice

            ActionForward forward = mapping.findForward( "success" );
            StringBuffer path = new StringBuffer( forward.getPath() );
            boolean isQuery = ( path.indexOf( "?" ) >= 0 );
            if( isQuery )
            path.append( "&oldcode=" );
            path.append(  details.getoldcode());
                path.append( "?oldcode=" );
                path.append( details.getoldcode() );
            return new ActionForward( path.toString() );

    where the parameter &oldcode is taking out of the url string in struts-config and replaced here

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now