Using Struts2, I have a very simple radio tag like following
<s:radio label="correctOption" name="correctAnswer" list="
#{'1':'1','2':'2','3':'3','4':'4'}" value="questionVo.correctAnswer"/>
questionVo.correctAnswer returns 2. So I want the second radio button to be preselected but it is not happening. I even tried:
<s:radio label="correctOption" name="correctAnswer" list="
#{'1':'1','2':'2','3':'3','4':'4'}" value="%{1}"/>
But that does not work either.
What am I doing wrong?
Remove the value attribute from the jsp. Then in your Java code make sure that the "correctAnswer" variable has the value you want.
This has also the added effect that works in postbacks (when the user has selected something on the radio and the form is shown again)
It works for me for the following:
<s:radio
label="correctOption"
name="correctAnswer"
list="#{'1':'1','2':'2','3':'3','4':'4'}"
value="%{1}"/>
I believe that the issue is that the value needs to be escaped properly eg:
value="%{'1'}"
and match the declaration exactly.
Here's the solution:
<s:radio label="correctOption" name="correctAnswer" list="
#{'1':'1','2':'2','3':'3','4':'4'}" value="1"/>
another one in case of strings in the static list:
<s:radio label="Gender" name="gender" list="
#{'male':'Male','female':'Female'}" value="'male'"/>
来源:https://stackoverflow.com/questions/1110558/preselect-radio-button-in-struts2