有没有办法将选项标记为默认选中,就像selected
标记中的HTML option
属性一样?{/ 1}?
答案 0 :(得分:6)
如果标签的路径值与options值的值匹配,则会自动选择它。你不需要任何特别的东西
答案 1 :(得分:2)
有没有办法将选项标记为默认选择???
只需使用< spring:选项Taglib 第一个将自动选中
<spring:select name="someProperty">
<spring:option value="">Select one</spring:option>
<spring:option value="someValue">Some value<spring:select>
<!--And so on...-->
<spring:select>
或
<spring:select name="someCollection">
<spring:option value="">Select one</spring:option>
<!--Here goes some List added to request-->
<spring:options itemLabel="propertyNameUsedAsLabel" itemValue="propertyNameUsedAsValue"/>
<!--And so on...-->
<spring:select>
答案 2 :(得分:1)
我假设你也在使用Spring MVC。如果您的业务逻辑需要默认选择某个选项,请将该业务逻辑移动到控制器 - 而不是JSP。
@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){
ModelAndView model = new ModelAndView("HelloWorldPage");
// first we need to give the countries list to the model
model.addObject("countries", countryService.getAllCountries());
// creating the form
ExampleForm form = new ExampleForm();
// setting the default to Germany (de)
form.setCountryCode = "de";
// adding the form (with the default country set) to the model
model.addObject("form", form);
return model;
}
在JSP中,我们将国家/地区传递给选项,春天将自动选择德国:
<form:form method="post" commandName="form">
<%-- other fields ... --%>
<form:select path="countryCode">
<form:options items="${countries}" itemValue="countryCode" itemLabel="countryName"/>
</form:select>
<%-- other fields ... --%>
</form:form>