Preselect radio button in Struts2

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

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'"/> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!