I can't tell you why the parameters are being passed out-of-sequence (I
would guess they're being shuffled through a HashMap somewhere) but it's
easy to deal with: simply have your setter grow the list as necessary to
accommodate the passed index. Something like this:
public void setFriend(int index, String name) {
while (friends.size() < index) {
friends.add(null); // or "" or whatever
}
friends.put(index, name);
}
HTH,
L.
venkat reddy wrote:
I've tried another example also.......
ListForm.java
=========
package com.myapp.struts.formbeans;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;
/**
*
* @author Sai
*/
public class ListForm extends ActionForm {
private int size = 3;
private List friends = new ArrayList(size);
public List getFriends( ) {
return friends;
}
public String getFriend(int index) {
if (index >= friends.size( )) {
friends.add(index, new String());
}
return (String) friends.get(index);
}
public void setFriend(int index, String name) {
System.out.println("size: "+friends.size()+"----- index:"+index);
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// prepopulate the list with empty strings
friends = new ArrayList( );
for (int i=0; i<size;i++) friends.add("");
}
}
pollsForm2.jsp
==============
<h2>List Form Test</h2>
<html:form action="/multipleForm" method="get">
Who are your 3 friends:<br />
Friend 1: <html:text property="friend[0]"/><br />
Friend 2: <html:text property="friend[1]"/><br />
Friend 3: <html:text property="friend[2]"/><br />
Friend 3: <html:text property="friend[3]"/><br />
Friend 3: <html:text property="friend[4]"/><br />
Friend 3: <html:text property="friend[5]"/><br />
Friend 3: <html:text property="friend[6]"/><br />
Friend 3: <html:text property="friend[7]"/><br />
<html:submit/>
</html:form>
The output while setting the request parameter values is as follows......
size: 3----- index:0
size: 3----- index:4
size: 3----- index:3
size: 3----- index:6
size: 3----- index:7
size: 3----- index:5
size: 3----- index:1
size: 3----- index:2
We can see here also struts is not settings the parameter values in order (
i mean according to the index ). It's trying to set object at 0 index,
jumping by trying to set at 4 index.
Why is this so??........is this problem solved in struts 1.2.9 ( i'm
currently using 1.2.7) ??
Any personal suggestions are also appreciated..........
Friends, please help
On Wed, May 7, 2008 at 1:30 AM, venkat reddy <[EMAIL PROTECTED]> wrote:
I wanted to find a way to handle dynamic number of arguments for my
poll-posting form which should be able to handle dynamic number of choice
fields.
So i maintained a list (pollChoices --> java.util.List type) for
maintaining dynamic number of choice fields.
==============================
PollForm.java
==============================
package com.myapp.struts.formbeans;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;
import org.apache.commons.lang.*;
/**
*
* @author Sai
*/
public class PollForm extends ActionForm {
private String pollQuestion;
private String numberOfDays;
private ArrayList pollChoices;
/** Creates a new instance of BasicForm */
public PollForm() {
}
public void reset(ActionMapping map, HttpServletRequest req) {
System.out.println("Reset start...........");
setPollQuestion("");
setNumberOfDays("");
pollChoices = new ArrayList();
System.out.println("Reset end...........");
}
public ActionErrors validate(ActionMapping map, HttpServletRequest req)
{
System.out.println("Validate method...........");
ActionErrors errors = new ActionErrors();
if(StringUtils.isBlank(pollQuestion))
errors.add("pollQuestion",new
ActionMessage("errors.required","Poll Question"));
if(StringUtils.isBlank(numberOfDays))
errors.add("numberOfDays",new
ActionMessage("errors.required","Number of days"));
if(pollChoices.size() < 2)
errors.add("pollChoice",new
ActionMessage("errors.required","Atleast two poll choices"));
return errors;
}
public String getPollQuestion() {
return pollQuestion;
}
public void setPollQuestion(String pollQuestion) {
this.pollQuestion = pollQuestion;
}
public String getNumberOfDays() {
return numberOfDays;
}
public void setNumberOfDays(String numberOfDays) {
this.numberOfDays = numberOfDays;
}
public String getPollChoice(int index) {
if(index >= pollChoices.size()) {
pollChoices.add(index,new String());
}
return (String) pollChoices.get(index);
}
public void setPollChoice(int index,String choice) {
System.out.println("size: "+pollChoices.size()+"-----
index:"+index);
if(index < pollChoices.size())
pollChoices.set(index,choice);
else
pollChoices.add(index,choice);
}
}
===============================================================================
The html-form looks like below
<html:form method="get" action="/multipleForm">
<html:text property="pollQuestion" /><br/><br/>
<html:text property="numberOfDays" /><br/><br/>
<html:text property="pollChoice[0]" /><br/><br/>
<html:text property="pollChoice[1]" /><br/><br/>
<html:text property="pollChoice[2]" /><br/><br/>
<html:text property="pollChoice[3]" /><br/><br/>
<html:text property="pollChoice[4]" /><br/><br/>
<html:text property="pollChoice[5]" /><br/><br/>
<html:text property="pollChoice[6]" /><br/><br/>
<html:text property="pollChoice[7]" /><br/><br/>
<html:submit />
</html:form>
=================================================
I'm using struts 1.2.7
Now the problem is when struts populates my pollChoice fields using
bean-utils setIndexedProperty method it's setting is starting from index 2.
I mean this is the output when i ran my program.
Reset start...........
Reset end...........
size: 0----- index:2
size: 0----- index:3
size: 0----- index:1
size: 0----- index:0
size: 0----- index:7
size: 0----- index:6
size: 0----- index:4
size: 0----- index:5
Validate method...........
Note: I've provided, System.out.println("size: "+pollChoices.size()+"-----
index:"+index); inside my PollForm.java to see how struts sets my form.
As i said i'm getting an error, that's because the size is 0 and the index
it's trying to set is greater than that.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]