我正在开发一个webapp,我想知道是否有机会在Spring MVC中实现以下HTML元素?
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
提前致谢!
答案 0 :(得分:2)
你可以在jsp页面上做任何你想做的事。
例如:
<form:form method="POST" commandName="recipe" action="${home}/admin/recipes">
<div class="form-group">
<label><s:message code="recipe.title" />*</label>
<form:input path="title" cssClass="form-control" />
<form:errors path="title" cssClass="error-help" />
</div>
<div class="form-group">
<s:message code="add" var="addLabel" />
<input type="submit" class="btn btn-success" value="${addLabel}" />
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
</span>
<input class="form-control" list="datalist" name="data" required placeholder="<s:message code='user.email' />" value="${userEmail}" />
<datalist id="datalist">
<option value="first" />
<option value="second" />
</datalist>
</div>
</div>
</form:form>
这里我们有模型对象&#34; recipe&#34;字段&#34;标题&#34;和字段&#34;数据&#34;与datalist
控制器代码:
@RequestMapping(value = "/admin/recipes", method = RequestMethod.POST)
public String add(
@ModelAttribute("recipe") @Valid Recipe recipe,
BindingResult bindingResult,
@RequestParam("data") String data,
Model model) {
if (bindingResult.hasErrors()) {
populateEditForm(model, recipe);
return "admin:/recipes/add";
}
System.out.println(data);
recipe = recipeService.create(recipe);
return "redirect:/admin/recipes/" + recipe.getId();
}
您可以选择&#34;首先&#34;或&#34;秒&#34;对于&#34;数据&#34;输入,它将在服务器端打印出来