This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

nested logic:iterate

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP
<html:form name="addresses" type="AddressActionForm" action="/Addresses">
<table>
<logic:iterate name="addresses" property="addressList" id="foo" indexId="ctr">
<tr>
<logic:iterate name="addresses" property='<%= "addressList[" + ctr + "]" %>' id="woo" indexId="pc">
<td>

<html:text name="addresses"
property='<%= "addressList[" + ctr + "][" + pc + "]" %>' />

</td>
</logic:iterate>
</tr>
</logic:iterate>
<html:submit property="submitValue">Submit Changes</html:submit>
</table>

</html:form>

AddressActionForm is the bean type.
When I do the above nested iterates the output is a 5 col x 3 rows (which it should be) but the textfields all have a binary address. The top row, all the fields are the address for addressList[0] the next row for addressList[1] etc.....

Bean
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import java.util.*;

public class AddressActionForm extends ActionForm {

private String[] hold1 = {"101 Sciarappa", "apt 2", "Cambridge", "MA", "02138"};
private String[] hold2 = {"1 booboo", "", "Somerville", "MA", "02143"};
private String[] hold3 = {"98 Appleton", "apt 3", "Cambridge", "MA", "02100"};
private String addressList[][] = {hold1, hold2, hold3};

public String[][] getAddressList()
{
return addressList;
}

public void setAddressList(String[][] str)
{
addressList = str;
}
}
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what is your problem?

In addition, could you eliborate more on this:


but the textfields all have a binary address



What do you mean about a "binary address"?

Nick
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think binary address means the reference address, not the value.

As I see, I think the problem is in addressList which is two dimensional array.
And you access it as one dimensional

 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David.

I see, you are right. The problem is, the address list is a 1-D array, which stores 3 String[]s.

Thus, if you use a 2-D representation, it shows the memory address (which should be the term binary address ) of the String[].

In such sense, you might use David's approach, or consider to create a 2-D String array, instead of a String array of String array.

Nick
 
Jason Goodman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More info.
The printout for all 5 textfields in the top row are:
[Ljava.lang.String;@4b4cae
For the second row they are all
[Ljava.lang.String;@176ee6

if I write
<html:text property='<%= "addressList[" + 0 + ]" %>' />
the printout is
[Ljava.lang.String;@4b4cae (makes me think the address of the string arrays within the array of strings is being printed).

Thanks
 
Jason Goodman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant Hex addresses.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try something like this
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, should this:


private String addressList[][] = {hold1, hold2, hold3};


really a 2-D array?

It is a 1-D array I feel, isnt it?

Nick
 
Jason Goodman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did try <%= "addressList[0][0]" %> and it gives me the same as <%= "addressList[0]" %>

Which really is
<html:text name="addresses"
property='<%= "addressList[" + ctr + "][" + pc + "]" %>' />

I think addressList[][] is a double array and I should be able to obtain the value held with [0][0]. It seems to me like the second 'dereference' is being ignored.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several issues.

First, you use a 2-D array to represent a 1-D array. Second, address[0] is NOT a String object, instead, it is a String array. If you just get the value of address[0], it represents the memory address of the String array hold1. However, address[0][0] is NOT equal to (address[0])(hold1[0]). Thus, either way wont work.

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to make it work, you might consider to declare address as a real 2-D array, for example:

String address[][] = {{"x1", "x2"}, {"y1", "y2"}};

Then, address[0][0] = "x1", address[0][1] = "x2", address[1][0] = "y1" and address[1][1] = "y2".

Nick
 
Jason Goodman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following in my bean
String address[][] = {{"x1", "x2"}, {"y1", "y2"}};

I still get the addresses instead of the value.

is this a problem with reflection in struts because a single level array works fine?
Thanks
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to change the addressList definition to

adressList[] = ...;
 
Jason Goodman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using addressList[] and trying to put arrays into fails on compile time.
When using addressList as a String array, and putting in {"address1", "address2" .....} then with a single iterate on the jsp page I do get the values "address1" "address2" to display. So it seems I am doing something wrong with the array of arrays or, Struts can't handle it.
But what I have moved to is, addressList is now an array of Address Beans.
Then in the jsp I can use a single iterate, and output the values held in the objects as follows:
<html:text name="addresses"
property = '<%= "stringIndexed[" + ctr + "].zip" %>' />

But I still am curious about why the [][] did not work. Using Beans means the jsp has to explicitly out put a street, city, state, etc...
With double arrays the jsp would only need the nested iterates so if in future another property was added to the bean, no changes would be needed on the jsp.
So, if you think of anything, great. But in either case, thank you for your help. It is very appreciated.
 
    Bookmark Topic Watch Topic
  • New Topic