【问题标题】:Spring form:select JSP tag always uses multipleSpring form:select JSP标签总是使用多个
【发布时间】:2016-02-22 14:33:15
【问题描述】:

我有一个使用 form:select 标记的 spring webapp。标签如下:

<c:forEach items="${info.formAreas}" var="area">
    <div class="area">
    <form:label path="areas">${area.name}</form:label>
    <form:select id="form_area_${area.id}" items="${area.options}" path="areas" class="area_select"/>
    </div>
</c:forEach>

其中areas 是我的表单绑定对象上的String[]${info.formAreas}AreaDTO 的列表,转载如下:

public class AreaDTO {

  private int id;
  private String name;
  private String shortName;
  private boolean dontmind;

  public Map<String,String> getOptions() {
    Map<String,String> options = new LinkedHashMap<String,String>();
    options.put(id+":0", "No");
    if (dontmind) options.put(id+":1", "Don't Mind");
    options.put(id+":2", "Yes");
    return options;
  }

  //other getters/setters
}

生成的 HTML 如下:

<div class="area">
    <label for="areas">An Area Name</label>
    <select id="form_area_1" name="areas" class="area_select" multiple="multiple">
      <option value="1:0" selected="selected">No</option>
      <option value="1:1">Don&#39;t Mind</option>
      <option value="1:2">Yes</option>
    </select>
    <input type="hidden" name="_areas" value="1"/>
 </div>

首先,为什么会产生multiple="multiple",我只想要一个单选下拉菜单,其次,隐藏的输入来自哪里?

【问题讨论】:

  • 似乎和这个问题一样(没有投票答案):stackoverflow.com/questions/12483849/…
  • 所以从上面的评论来看,如果你的areas 变量包含多个值(例如List),那么你会得到一个多选。
  • @Tobb 是的 - 就是这样!
  • 我会添加它作为答案然后..

标签: spring jsp


【解决方案1】:

您是否获得单选或多选取决于用于保存所选内容的变量类型(path-变量)。如果这是某种类型的集合(例如 List),它将成为多选。

隐藏的输入字段可能只是 Spring 在内部使用的东西,也许是为了在未选择任何内容时发布一些值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2017-12-25
    • 2014-12-10
    相关资源
    最近更新 更多