如何让 Struts 单选标签创建单选按钮的垂直列表

2022-01-16 00:00:00 html css java jsp struts2

我正在使用一个 struts 单选标签,该标签填充了具有两个字段的对象列表:

类 MyAction {列出<我的对象>我的清单;字符串 selectedId公共字符串执行(){...myList = new ArrayList();myList.add(new MyObject("1","first object");myList.add(new MyObject("2","第二个对象");myList.add(new MyObject("3","第二个对象");...}//myList & 的 Getter 和 Setter选定的Id...}类我的对象 {字符串标识;字符串名称;我的对象(字符串 id,字符串名称){这个.id = id;this.name = 名称;}//id & 的 Getter 和 Setter名称...}

这是我在页面上用来显示单选按钮列表的内容

<s:radio key="selectedId" list="myList" listKey="id" listValue="name"/>

但是,这会生成单选按钮的水平列表.我尝试为它们添加 css 样式:

<style>.垂直输入{显示:块;}</风格>

但这会导致标签和单选按钮也显示在不同的行上,而不是单选按钮和标签显示在同一行:

<块引用>
  • 第一个对象
  • 第二个对象
  • 第三个对象

    我想要的是:

    <块引用>
  • 第一个对象
  • 第二个对象
  • 第三个对象
  • 解决方案

    其实很简单,我的意思是使用主题 simple :)

    <s:iterator value="myList"><s:radio theme="simple" name="someNameToSubmit" list="#{id:name}"/><br></s:迭代器>

    这将使 name 作为标签,id 作为要提交的属性

    I'm using a struts radio tag that is being populated with a list of objects that have two fields:

    class MyAction {
         List<MyObject> myList;
         String selectedId
         public String execute() {
             ...
             myList = new ArrayList<MyObject>();
             myList.add(new MyObject("1","first object");
             myList.add(new MyObject("2","second object");
             myList.add(new MyObject("3","second object");
             ...
         }
    
         // Getters and Setters for myList & selectedId
         ...
    }
    
    class MyObject {
        String id;
        String name;
    
        MyObject(String id, String name) {
             this.id = id;
             this.name = name;
        }
        // Getters and Setters for id & name
        ...
    }
    

    Here's what I was using on my page to display the list of radio buttons

    <s:radio key="selectedId" list="myList" listKey="id" listValue="name"/>
    

    However, this yields a horizontal list of radio buttons. I tried adding a css style to them:

    <style>
        .vertical input { display: block; }
    </style>
    

    But this causes the labels and the radio buttons to show up on separate lines as well, instead of the radio button and label on the same line:

  • first object
  • second object
  • third object

    what I want is:

  • first object
  • second object
  • third object
  • 解决方案

    its actually simple, i mean use theme simple :)

    <s:iterator value="myList"> 
      <s:radio theme="simple" name="someNameToSubmit" list="#{id:name}"/><br>
    </s:iterator> 
    

    This will make name as a label and id as the property to submit

    相关文章