java 带有空白默认值的 JSP 下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17746384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:52:04  来源:igfitidea点击:

JSP Dropdown List with Blank Default

javajsp

提问by EdgeCase

I have a JSP Dropdown that is populated from a database query, and it is working just fine.

我有一个从数据库查询填充的 JSP Dropdown,它工作得很好。

But I want to also add a blank default option so that the field is empty when submitted. Right now the default is the first value selected from the database. But I want it to appear empty.

但我还想添加一个空白的默认选项,以便提交时该字段为空。现在默认值是从数据库中选择的第一个值。但我希望它看起来是空的。

The only way I've gotten it to work is to manually add a ""to the list as it is being populated.

我让它工作的唯一方法""是在填充列表时手动将其添加到列表中。

Is there a more elegant way of doing that?

有没有更优雅的方式来做到这一点?

        <div class="row">
            <label class="span2">Action</label>
            <form:select path="Action">
                <form:options items="${actionList}" />
                <form:errors path="Action" cssClass="error"/>
            </form:select>
        </div>

Page with default values, which I do not want to appear by default:

带有默认值的页面,我不想默认显示:

enter image description here

在此处输入图片说明

回答by Andrea Ligios

Use single option element

使用单个选项元素

<form:select path="Action" >
    <form:option  value="" />
    <form:options items="${actionList}" />
    <form:errors  path="Action" cssClass="error"/>
</form:select>

回答by Grim

An empty default is no good UI-Design. You should give a hint what to do.

空的默认值不是好的 UI 设计。你应该给一个提示该怎么做。

    <div class="row">
        <label class="span2">Action</label>
        <form:select path="Action">
            <option selected="selected"> -- Please choose --</option>
            <form:options items="${actionList}" />
            <form:errors path="Action" cssClass="error"/>
        </form:select>
    </div>

Or better html is

或者更好的 html 是

    <div class="row">
        <label class="span2">Action
            <form:select path="Action">
                <option selected="selected"> -- Please choose --</option>
                <form:options items="${actionList}" />
                <form:errors path="Action" cssClass="error"/>
            </form:select>
        </label>
    </div>

If you like to style it like a table, you should remove the div, style the label as display:table-cell and ...

如果您喜欢将其样式设置为表格,则应删除 div,将标签样式设置为 display:table-cell 和 ...

回答by munkiepus

As the first answer is not particularly UX friendly and the second answer is not very spring-y :/

由于第一个答案对用户体验不是特别友好,而第二个答案不是很有弹性:/

Combining both the previous answers would give best of both worlds:

结合前面的两个答案将两全其美:

<form:select path="Action" >
  <form:option  value="">--Select one--</form:option>
  <form:options items="${actionList}" />
  <form:errors  path="Action" cssClass="error"/>
</form:select>

and this will store a null value in the database.

这将在数​​据库中存储一个空值。