当前位置: 代码迷 >> Java Web开发 >> struts2 单选框有关问题
  详细解决方案

struts2 单选框有关问题

热度:5137   发布时间:2013-02-25 21:22:25.0
struts2 单选框问题
假设我有一个单选框在前台,
HTML code
            <s:form  name="targetForm" action="login">            <s:textfield name="username" label="用户名"></s:textfield>             <s:password name="password" label="密 码"></s:password>              <s:radio label="用户类型" name ="type" list= "{'学生','管理员'}"/>            <s:submit value="登陆" />                    </s:form>

  那么的话,我这个单选框
 
HTML code
<s:radio label="用户类型" name ="type" list= "{'学生','管理员','游客'}"/>

  里面的值有三个,我在后台的action里面获取不到单选框的值吗?
  例如我这么做:
Java code
public class LoginAction extends BaseAction{        private String type;    ……………………       //省略无关变量的setter    public String getType()    {        return type;    }    public void setType(String type)    {        this.type = type;    }

  然后就会报错:
严重: Servlet.service() for servlet jsp threw exception
tag 'radio', field 'list', name 'type': The requested list key '{'学生','管理员''}' could not be resolved as a collection/array/map/enumeration/iterator type[color=#FF0000][/color]. Example: people or people.{name} - [unknown location]

  1,难道不是用String类存放单选框的值吗?
  2,如何设置让单选框的默认选中 管理员那个选项呢?

------解决方案--------------------------------------------------------
探讨

<s:radio label="用户类型" name ="type" list= "#{'student':‘学生','admin':'管理员','tourist':'游客'}"/>
这样写 加上key
  相关解决方案