struts radio indexed field

P

Pavel

Greetings to all -

I'm having some difficulty to render indexed radio input fields
correctly. The problem is that it's a "double" indexed example. The
jsp operates on the collection of the Question instances. And each
question has a list of pre-defined answers to choose from. The idea is
to list all of the questions and then collect the answers chosen. The
"list" and "text" types are working (generating both
name="questions[x].answer", where x is the index of the question) but
radio does not.

If indexed="true" is specified, then each pre-defined radio answer has
a name="questions[x].answer" WHERE x is the index of the PRE-DEFINED
ANSWER from the predefinedAnswers iterator (not from the questions
iterator).

If indexed attribute is not specified, then the input field name is
rendered just as name="answer".

There should be a way to make it work. Is nested:radio going to help
in any way ? Any suggestions are very appreciated.
Thanks
Pavel

Here is the relevant portion of the jsp:

<logic:iterate id="questions" property="questions"
name="questionSetForm">

<logic:equal name="questions" property="type" value="list">
<div>It's a list</div>
<html:select name="questions" property="answer" indexed="true">
<html:eek:ptionsCollection name="questions"
property="predefinedAnswers" value="id" label="label"/>
</html:select>
</logic:equal>

<logic:equal name="questions" property="type" value="text">
<div>It's a text</div>
<html:text name="questions" property="answer" indexed="true" />
</logic:equal>

<logic:equal name="questions" property="type" value="radio">
<div>It's a radio choice</div>
<logic:iterate id="predefinedAnswer" property="predefinedAnswers"
name="questions">
<html:radio name="questions" property="answer" value="id"
idName="predefinedAnswer">
<bean:write name="predefinedAnswer" property="label" />
</html:radio>
</logic:iterate>
</logic:equal>

</logic:iterate>

The questionSetForm class defines only one property that hold a
collection of Question instances:

public List getQuestions() {
return questions;
}

public void setQuestions(List questions) {
this.questions = questions;
}

The Question class defines the following relevant properties (along
with corresponding get/set methods):
private Integer id = null;
private Integer questionSetId = null;
private String type = null;
//Holds the collection of the PredefinedAnswer instances
private List predefinedAnswers = null;
private String label = null;
private String answer = null;

The PredefinedAnswer class defines the id and label properties used to
render the list of choices.
 
P

Pavel

Adding an indexId attribute to the first iterator and then using its
value in the radio element's property is the only solution that comes
to mind so far. I do not like to use scriplet code but I'm not sure
that there is a better solution at this time in the absence of the
RadioGroup tag.

<logic:iterate id="questions" property="questions"
name="questionSetForm" indexId="index">

....
<html:radio property="<%= \"questions[\"+ index
+\"].answer\"%>" value="id" idName="predefinedAnswer">
....

Any alternative solution is appreciated.
Thanks
Pavel

Greetings to all -

I'm having some difficulty to render indexed radio input fields
correctly. The problem is that it's a "double" indexed example. The
jsp operates on the collection of the Question instances. And each
question has a list of pre-defined answers to choose from. The idea is
to list all of the questions and then collect the answers chosen. The
"list" and "text" types are working (generating both
name="questions[x].answer", where x is the index of the question) but
radio does not.

If indexed="true" is specified, then each pre-defined radio answer has
a name="questions[x].answer" WHERE x is the index of the PRE-DEFINED
ANSWER from the predefinedAnswers iterator (not from the questions
iterator).

If indexed attribute is not specified, then the input field name is
rendered just as name="answer".

There should be a way to make it work. Is nested:radio going to help
in any way ? Any suggestions are very appreciated.
Thanks
Pavel

Here is the relevant portion of the jsp:

<logic:iterate id="questions" property="questions"
name="questionSetForm">

<logic:equal name="questions" property="type" value="list">
<div>It's a list</div>
<html:select name="questions" property="answer" indexed="true">
<html:eek:ptionsCollection name="questions"
property="predefinedAnswers" value="id" label="label"/>
</html:select>
</logic:equal>

<logic:equal name="questions" property="type" value="text">
<div>It's a text</div>
<html:text name="questions" property="answer" indexed="true" />
</logic:equal>

<logic:equal name="questions" property="type" value="radio">
<div>It's a radio choice</div>
<logic:iterate id="predefinedAnswer" property="predefinedAnswers"
name="questions">
<html:radio name="questions" property="answer" value="id"
idName="predefinedAnswer">
<bean:write name="predefinedAnswer" property="label" />
</html:radio>
</logic:iterate>
</logic:equal>

</logic:iterate>

The questionSetForm class defines only one property that hold a
collection of Question instances:

public List getQuestions() {
return questions;
}

public void setQuestions(List questions) {
this.questions = questions;
}

The Question class defines the following relevant properties (along
with corresponding get/set methods):
private Integer id = null;
private Integer questionSetId = null;
private String type = null;
//Holds the collection of the PredefinedAnswer instances
private List predefinedAnswers = null;
private String label = null;
private String answer = null;

The PredefinedAnswer class defines the id and label properties used to
render the list of choices.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top