Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Iterating with struts

Posted on 2011-10-29
Medium Priority
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
  • 3

Accepted Solution

dhruvpratap earned 2000 total points
ID: 37052516
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

ID: 37053928
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="com.partners.action.detailsAction" 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

ID: 37053932
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

ID: 37053999
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you a startup company? Being a startup, you may be using shared hosting, or maybe even dedicated hosting. But have you ever given a thought to using cloud computing now? Yes, don’t be surprised, it is possible for startups to opt for cloud compu…
If you try to migrate from Elastix to Issabel, you will face a lot of issues. These problems are inevitable but fortunately, you can fix them. In the guide below, I will explain how I performed the migration while keeping all data and successfully t…
Screencast - Getting to Know the Pipeline
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

569 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