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.