可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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)
回答2:
It works for me for the following:
<s:radio label="correctOption" name="correctAnswer" list="#{'1':'1','2':'2','3':'3','4':'4'}" value="%{1}"/>
回答3:
I believe that the issue is that the value needs to be escaped properly eg:
value="%{'1'}"
and match the declaration exactly.
回答4:
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'"/>