1
我使用Spring mvc作爲我的web應用程序。我有一個簡單的表單,包含一些文本框和組合框。組合框沒有填充表單驗證失敗後 - Spring 3 MVC
問題:我正在提交空值的表單並執行驗證。由於空值存在,因此驗證會再次顯示窗體,但會顯示空白組合框。當表單第一次顯示時,一切正常。
這是代碼。我如何用最後選定的值填充它們?
瀏覽文件
<form:form method="post" modelAttribute="screening">
<table>
<!-- Text Form -->
<tr>
<td>EPI ID</td>
<td><form:input path="epi_Id" /></td>
<td><form:errors path="epi_Id" cssClass="error" /></td>
</tr>
<tr>
<!-- Combo-Box -->
<td>Enrollment Center</td>
<td>
<form:select path="enrollmentCenter">
<form:options items="${epiCentersList}" itemValue="centerID" itemLabel="name"/>
</form:select>
</td>
<td><form:errors path="enrollmentCenter" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
控制器
@Controller
@RequestMapping("/ScreeningForm")
@SessionAttributes("screening")
public class ScreeningFormController {
@RequestMapping(method=RequestMethod.GET)
public String setupForm(Model model)
{
ScreeningDomain screen=new ScreeningDomain();
model.addAttribute("screening", screen);
model.addAttribute("epiCentersList", usc.getAllEpiCentersList());
return "ScreeningForm";
}
@RequestMapping(method=RequestMethod.POST)
public String submitForm(@ModelAttribute("screening") ScreeningDomain screeningDomain, BindingResult result, SessionStatus status)
{
ScreeningServiceImpl screenService = new ScreeningServiceImpl();
screeningValidator.validate(screeningDomain, result);
if (result.hasErrors())
{
UtilityServiceClass usc=new UtilityServiceClass();
model.addAttribute("epiCentersList", usc.getAllEpiCentersList());
model.addAttribute("screening", screeningDomain);
return "ScreeningForm";
}
else
{
screenService.add(screeningDomain);
return "redirect:ScreeningForm";
}
}
}
其他一切工作正常。 我的模型是丟失列表對象「epiCentersList」值,但我不知道爲什麼。
你不必'UtilityServiceClass USC =新UtilityServiceClass();在setupForm()中?這一行將刪除submitForm()中的所有EpiCentersList。檢查是否需要刪除!? – Vinay