STRUTS logic:iterate

M

MP

Hi everyone:

I'm having a problem getting the struts iterate to work. In my JSP I
have:

<logic:iterate id="template" name="ad_template_list"
property="templateListRecList"
type="com.package1.TemplateListRec">
<LI><bean:write name="template" property="templateId"/></LI>
</logic:iterate>

ad_template_list maps to a form bean in the struts_config.xml file
templateListRecList will call the getter method getTemplateListRecList
and returns a Hashtable which contains a key and a TemplateListRec
object.

So I set the type to be com.package1.TemplateListRec which should set
that type.

However when I run it I get the error:

Error: 404 SC_NOT_FOUND
Extension of file not found, while displaying static content

When I take out the type attribute then it runs fine and will print
the object on the screen: com.package1.TemplateListRec@244023d

So obviously the hashtable contains that object why cannot I not set
it to the type specified.

In the struts logic API it says under type: The actual elements of the
collection must be assignment-compatible with this class, or a request
time ClassCastException will occur

I think this is what is happening. This is what mine looks like:

package com.package1.adbuilder;


public final class TemplateListRec {

public TemplateListRec(String templateId, String imagePath, String
summaryText){
this.templateId = templateId;
this.imagePath = imagePath;
this.summaryText = summaryText;
}

public TemplateListRec() {
}

public String getTemplateId() { return (this.templateId); }
public void setTemplateId(String templateId) { this.templateId =
templateId; }

public String getImagePath() { return (this.imagePath); }
public void setImagePath(String imagePath) { this.imagePath =
imagePath; }

public String getSummaryText() { return (this.summaryText); }
public void setSummaryText(String summaryText) { this.summaryText
= summaryText; }

private String templateId;
private String imagePath;
private String summaryText;
}

Can anyone see why this would be happening?

Thanks in advance!
 
S

Sudsy

MP wrote:
In the struts logic API it says under type: The actual elements of the
collection must be assignment-compatible with this class, or a request
time ClassCastException will occur
<snip>

Yes, but it also says:

"Normally, each object exposed by the iterate tag is an element of the
underlying collection you are iterating over. However, if you iterate
over a Map, the exposed object is of type Map.Entry that has two properties:

* key - The key under which this item is stored in the underlying Map.
* value - The value that corresponds to this key.

So, if you wish to iterate over the values of a Hashtable, you would
implement code like the following:
<logic:iterate id="element" name="myhashtable">
Next element is <bean:write name="element" property="value"/>
</logic:iterate>"

Now, if you want me to write the code for you... ;-)
 
M

MP

Thanks.

I had that implemented but since the value in my hashtable is an
object it only would print the object. I need to use the getter
methods within that object to display the appropriate values. Once I
assign the <bean:write name="element" property="value"/> can I set
that type?

I see how it would work if in the hash I had ("one","hello") however
since I have ("one", myObject) I can't figure out how it could work.

Thanks again.
 
S

Sudsy

MP said:
Thanks.

I had that implemented but since the value in my hashtable is an
object it only would print the object. I need to use the getter
methods within that object to display the appropriate values. Once I
assign the <bean:write name="element" property="value"/> can I set
that type?

I see how it would work if in the hash I had ("one","hello") however
since I have ("one", myObject) I can't figure out how it could work.

Hint #2: Look into the bean:define tag. Perhaps something like this:

<logic:iterate id="template" name="ad_template_list"
property="templateListRecList">
<bean:define id="listrec" name="template" property="value"
type="com.package1.TemplateListRec" />
<li><bean:write name="listrec" property="templateId" /></li>
</logic:iterate>

Please remit my fee via PayPal. ;-)

ps. Search the archives and you should find links to some of my
articles on advanced Struts. The learning curve is still steep
but the gradient is decreasing.
 
R

Ryan Stewart

MP said:
Thanks.

I had that implemented but since the value in my hashtable is an
object it only would print the object. I need to use the getter
methods within that object to display the appropriate values. Once I
assign the <bean:write name="element" property="value"/> can I set
that type?

I see how it would work if in the hash I had ("one","hello") however
since I have ("one", myObject) I can't figure out how it could work.
In addition to the Great and Knowledgeable Sudsy's comments, have a look at
JSTL and/or expression language. JSTL is recommended for use over the Struts
tags when appropriate. Expression language (within or without JSTL) allows
you to get object properties and many other useful things.
 
S

Sudsy

Sudsy wrote:
<logic:iterate id="template" name="ad_template_list"
property="templateListRecList">
<bean:define id="listrec" name="template" property="value"
type="com.package1.TemplateListRec" />
<li><bean:write name="listrec" property="templateId" /></li>
</logic:iterate>

Now that I think about it, this code SHOULD work (although I don't
have time to test it right now):

<logic:iterate id="template" name="ad_template_list"
property="templateListRecList">
<li><bean:write name="template" property="value.templateId" /></li>
</logic:iterate>
 
Joined
Aug 17, 2007
Messages
1
Reaction score
0
pass a map back from a form using logic:iterate or nested:iterate and struts hidden

Hi all. This answer isnt directly related to this question, but when you search for "logic:iterate hashmap" this is one of the first threads that comes up in google.

I spent way to much time trying to figure out how to pass a map of objects into and back from a form so i wanted to make sure i tried to save someone else the time i just spent.

Arrived at the solution accidentally - hope its not specific to my version of struts (1.1). Maybe its documented somewhere?

/////code in jsp/////
<nested:root name="aForm">
<nested:iterate property="filterSongs">
<nested:hidden property="" />
</nested:iterate>
</nested:root>

/////output from code in jsp/////
<input type="hidden" name="filterSongs(1968)" value="MWC 506 SONG">
<input type="hidden" name="filterSongs(2186)" value="MWC 506 SONG 2">
<input type="hidden" name="filterSongs(2185)" value="MWC 506 SONG 2">


This jsp code will allow you to submit a map back to the server in a form. The important bit is to include the property attribute in the hidden tag, but to leave it blank. Struts takes it from there. I believe it will work at any nesting level. The actionform must implement a reset method that uses MapUtils.LazyMap:

public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
filterSongs = MapUtils.lazyMap(new java.util.HashMap(), new Factory() {
public Object create() { return new String(); }
});
}

Havnt really tested this anywhere else. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top