Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts2 list of radio buttons

 
Jordan Smith
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
i have a list of questions. each question object is declared like this:


each Answer is declared like this:

i want to iterate of the questions and display them like this:
1. How was your day?
<input type="radio" name="group1" value="1">Very good
<input type="radio" name="group1" value="2">Moderate
<input type="radio" name="group1" value="3">Horrible
2. Who is could be your best friend?
<input type="radio" name="group1" value="1">Mother
<input type="radio" name="group1" value="2">Father
<input type="radio" name="group1" value="3">Sister
<input type="radio" name="group1" value="3">Brother

I would like it to save for each question what answer was chosen.

How can I do it please?
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't specify how is the question declared in your action, I suppose there is a list of Questions in your action class. So you need to iterate over the questions, print the question one by one along with the options using the radio tag like this



I think that should work, I didn't test it. When you submit the form back, you need to catch the radio button values into a List or array. See the struts 2 tag reference for more info on the tags...
 
Jordan Smith
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
How can I catch the radio button values into a List or array?
thanks
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just declare a list or array of name answer in the target action...
 
Jordan Smith
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
I was trying your solution
but i get the following error:

Error on line 25, column 9 in template/simple/radiomap.ftl
stack.findValue(parameters.listKey) is undefined.
It cannot be assigned to itemKey
The problematic instruction:
----------
==> assignment: itemKey=stack.findValue(parameters.listKey) [on line 25, column 9 in template/simple/radiomap.ftl]
in user-directive s.iterator [on line 23, column 1 in template/simple/radiomap.ftl]
----------

Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Error on line 25, column 9 in template/simple/radiomap.ftl
stack.findValue(parameters.listKey) is undefined.
It cannot be assigned to itemKey
at freemarker.core.Assignment.accept(Assignment.java:111)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.IfBlock.accept(IfBlock.java:82)


why is it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic