Re: Why does struts assign indexedValues from 2nd index.......please help
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; iList Form Test Who are your 3 friends: Friend 1: Friend 2: Friend 3: Friend 3: Friend 3: Friend 3: Friend 3: Friend 3: 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
Re: Why does struts assign indexedValues from 2nd index.......please help
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; iList Form Test Who are your 3 friends: Friend 1: Friend 2: Friend 3: Friend 3: Friend 3: Friend 3: Friend 3: Friend 3: 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 > > > > > > > > > > > > > > > > = > I'm using struts 1.2.7 > > Now the problem is when struts populates my pollChoice fields using > bean-utils setIndexedPrope