aspose file tools*
The moose likes Struts and the fly likes Retrieving strings using getStrings() - DynaValidatorActionForm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Retrieving strings using getStrings() - DynaValidatorActionForm" Watch "Retrieving strings using getStrings() - DynaValidatorActionForm" New topic
Author

Retrieving strings using getStrings() - DynaValidatorActionForm

Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
In my application, i've a requiremet for adding text boxes dynamically.
I've done this using java Script.
But storing them to my Form bean - DynaValidatorActionForm is becoming a probelm. I'm using following approach.
I've a String[] property in my form. and in JSP, i've a hidden field associated with it. Whenever i add a text box dynamically in JSP, i'll store that particualr entry in that hidden field.

Compilation is fine. but When i execute this, i'm getting java.lang.NoSuchMethodError: org.apache.struts.action.DynaActionForm.getStrings(Ljava/lang/String

Exception is at this point :
String[] bugs=actionForm.getStrings("bugList");

I was using struts 1.1 earlier. but as this feature is in struts 1.2, i've upgraded to struts 1.2.

what could be the problem?

Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Try

String[] bugs= (String[])actionForm.get("bugList");


Merrill
Consultant, Sima Solutions
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
I've tried that too...
It gives me only first element of array.
becoz acitionForm.get() returns a String, not String[].
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
dynaActionForm.get returns whatever you've defined for the element.

So, if you defined:

<form-property name="bugList" type="java.lang.String[]">

It will return a String array.

The problem may also be in how you're putting the array in the form. If you're using javascript to do this, you can't just put a javaScript array into a single hidden field. You must create one hidden field for each value, and name each field with the same name.

Here's an example:



After submitting the form with the above code, Struts will populate "bugList" as a string array.
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
I've tried using ur method.
Everytime it is trying to overwrite the existing value. and even that last value is also not getting saved.
but the array length in Action is perfect but not the values. values r always null.

I've modified the code a bit. and now i'm able to submit single set of values.
but the requirement is ability to add multiple bugs at a time.

here is my code

function addBugs()
{
var tbl = document.getElementById('requestTable');
var lastRow = tbl.rows.length;
var iteration = lastRow+1;
var row = tbl.insertRow(lastRow);

var cell0 = row.insertCell(0);
var textNode = document.createTextNode('BugID :');
cell0.appendChild(textNode);

var cell1 = row.insertCell(1);
var el1 = document.createElement('input');
el1.type = 'text';
el1.size = 15;
//createHiddenField("bugList", el1.value);
//document.forms[0].bugList.value=el1.value;
cell1.appendChild(el1);

var cell2 = row.insertCell(2);
var textNode = document.createTextNode('Description :');
cell2.appendChild(textNode);

var cell3 = row.insertCell(3);
var el3 = document.createElement('input');
el3.type = 'text';
//createHiddenField("bugDescList", el3.value);
//document.forms[0].bugDescList.value=el1.value;
el3.size = 15;
cell3.appendChild(el3);

var cell4 = row.insertCell(4);
var el4 = document.createElement('input');
el4.type = 'button';
el4.value='Add'
el4.size = 15;
cell4.appendChild(el4);
}

Can U suggest any other alternate?

Sorry for not putting code in [code]. i was getting some problems with HTML tags.
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
this is onclick event on el4

document.RequestForm.bugList.value=el1.value;
//document.RequestForm.bugDescList.value=el3.value;
//submit the form here
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Try making your input fields "indexed". Here's an example:

Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
if we say bugList[i], doesn't it look for matching property in DynaForm??
I've tried . getting beanUtils.populate Exception .
but Thanks for ur replies.
any other alternate we can resolve this?

how abt Struts Indexed Properties? I've read some docs on that. My understanding is we can use that only inside <logic:iterate> tag. so I can't make use of this. rt??
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The solution I gave you in my last post does use Struts indexed properties. By giving a property name of "bugList[0]", you are telling the Struts that this is an indexed property. Here is a link that explains it.

Try using this same solution, except this time, specify org.apache.struts.validator.LazyValidatorForm as the class name for the form bean. I haven't tried it, but I believe this form is supposed to create whatever structure you need "on the fly" without any predefinition.
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
Thanks a lot for all ur replies.
This is the only place, where i'm getting some clue abt this.

if we want to use indexed properties, we have to add attribute indexed="true" to html:text tag.
but ... as we r using java script, we can't make use of html:text and indexed attribute. and moreover we can use indexed only inside <iterate> tag.

is there any way to resolve this in struts without using javascript??
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
Thanks a lot for all ur replies.
This is the only place, where i'm getting some clue abt this.

if we want to use indexed properties, we have to add attribute indexed="true" to html:text tag.
but ... as we r using java script, we can't make use of html:text and indexed attribute. and moreover we can use indexed only inside <iterate> tag.

is there any way to resolve this in struts without using javascript??
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
Thanks a lot for all ur replies.
This is the only place, where i'm getting some clue abt this.

if we want to use indexed properties, we have to add attribute indexed="true" to html:text tag.
but ... as we r using java script, we can't make use of html:text and indexed attribute. and moreover we can use indexed only inside <iterate> tag.

is there any way to resolve this in struts without using javascript??
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
if we want to use indexed properties, we have to add attribute indexed="true" to html:text tag.

Adding indexed="true" to the tag is only one way to use indexed properties. Giving the property a name with an index is another. Carefully read the link I gave you and you'll understand how it works.

but ... as we r using java script, we can't make use of html:text and indexed attribute


Not true. You can use JavaScript along with an <html:text> tag. You just have to remember that your JavaScript code is operating on the rendered version of the tag, or in other words the <input type="text"> tag that is generated by Struts before the page is displayed. To see the rendered tags, use your browser's "view source" function.

and moreover we can use indexed only inside <iterate> tag.


Again, not true. Here is an example of using indexed tags without an iterate tag:

<html:text property="myBean[0].name" />
<html:text property="myBean[1].name" />
<html:text property="myBean[2].name" />
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
Hi Higginson,
After trying all options... I gave up.
i had choice of changing the requirement. so i utilised that.
for time being, i gave up.
but i'll tryout to get the solution for this.
anyways ...thanks for the tips U gave me.
 
Don't get me started about those stupid light bulbs.
 
subject: Retrieving strings using getStrings() - DynaValidatorActionForm