2
我遇到了验证窗体的问题,窗体有很多不同的字段(输入文本字段,选择选项,多选择选项...)。我已经用spring security开发了关于验证表单的所有逻辑源代码,但是当验证表单失败(例如,用户不填写任何内容)并重新加载表单时,所有输入文本字段都被正确填充,但是选择选项和多个选择选项已被删除,用户必须重新选择。为什么不填充文本字段?Spring MVC选择窗体中的值:在窗体验证错误后选择
这是我的一块视图:
<tr>
<td><form:label path="TechContactName"><spring:message code="label.techcontactname"/><sup>*</sup></form:label></td>
<td><form:input path="TechContactName" /></td>
<td><form:errors path="techContactName" cssClass="error" /></td>
</tr>
<tr>
<td><form:label path="LicenseProduct"><spring:message code="label.licenseproduct"/><sup>*</sup></form:label></td>
<td><form:select path="licenseProduct" id="selectProduct" size="6" >
<option >Option 1</option>
<option >Option 2</option>
<option >Option 3</option>
<option >Option 4</option>
<option >Option 5</option>
<option >Option 6</option>
</form:select></td>
<td><form:errors path="licenseProduct" cssClass="error" /></td>
</tr>
<tr>
<td><form:label path="DeployCountries"><spring:message code="label.deploycountries"/><sup>*</sup></form:label></td>
<td><form:select path="deployCountries" id="selectCountries" multiple="multiple" >
<option value="AT">Austria</option>
<option value="BE">Belgium</option>
<option value="DK">Denmark</option>
<option value="FI">Finland</option>
<option value="FR">France</option>
<option value="DE">Germany</option>
</form:select> </td>
<td><form:errors path="deployCountries" cssClass="error" /></td>
</tr>
而我的控制器:
@SuppressWarnings("finally")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addLicense(@Valid @ModelAttribute(value="license") LicenseEntity license, BindingResult result, ModelMap map, HttpServletRequest request)
{
if(result.hasErrors()){
System.out.println("validation errors size..."+result.getErrorCount());
return "editLicenseList";
} else {
//all works correctly
}
}
的形式输入文本字段SI当验证失败时正确填写但形式:选择和形式:选择多个失败并用选定的任何值重新加载表单。
在此先感谢!
感谢威尔基林非常多!简单的选择完美的作品,但多选择不,如果我选择了多个选项后验证表单,并重新加载它没有选择任何东西,有什么办法来保持选择? – pedrojo05
您可以使用调试器检查提交表单后模型中的'LicenseEntity.deployCountries'列表是否已填充?这个列表的值需要匹配''标签中'value'的值 - 因此一个或多个字符串'AT','BE'等 –