spring 向表单添加默认值:选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2766382/
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-09-08 04:03:50  来源:igfitidea点击:

spring add default value to form:select

springspring-mvc

提问by Mike

I'm developing a spring application, now I've added a dropdownlist to one of my jsp pages using:

我正在开发一个 spring 应用程序,现在我使用以下命令向我的一个 jsp 页面添加了一个下拉列表:

<form:select multiple="single" path="users[y.count-1].X" items="${Y}" itemValue="id" itemLabel="name"/>

Now I'd like to add the default value "Nothing selected", however I can't seem to find how to do this. I've tried:

现在我想添加默认值“未选择任何内容”,但是我似乎无法找到如何执行此操作。我试过了:

<form:select multiple="single" path="users[y.count-1].X" items="${Y}" itemValue="id" itemLabel="name">
   <form:option value="Nothing selected" />
</form:select>

But "Nothing selected" isn't displayed in my dropdownlist.

但是“未选择任何内容”未显示在我的下拉列表中。

回答by Jacob Mattison

You should be able to do

你应该能够做到

<form:select multiple="single" path="users[y.count-1].X" >
   <form:option value="Nothing selected" />
   <form:options items="${Y}"  itemValue="id" itemLabel="name" />
</form:select>

回答by zygimantus

Just add this line before your options:

只需在您的选项之前添加此行:

<form:option selected="true" value="..." />