2
我現在有這樣的過程,通過初始表單頁面上傳一個文件的Spring MVC的servlet。處理Spring MVC異常作爲驗證錯誤
請求處理程序已經做了一些驗證的文件,如果缺少一定的要求,但不幸的是它不能很容易地分辨,如果所有的要求都滿足,直到處理實際發生。
@RequestMapping(path = "/", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String submit(@Valid FormData form, BindingResult result, Model model) throws IOException, ParseException
{
if (result.hasErrors())
{
return "index";
}
processFile(form.getFile());
return "success";
}
如果發生在處理步驟的異常,我處理它在@ExceptionHandler
註解的方法。但是,這種方法需要具有用於顯示在JSP頁面中的誤差的第二行:該方法本身看起來像
@ExceptionHandler(Exception.class)
public String databaseError(Model model, Exception e)
{
model.addAttribute("formData", new FormData());
model.addAttribute("error", "File failed to process. Please verify the contents of the file.");
return "index";
}
<form:input type="file" name="file" path="file" value=""/>
<form:errors path="file" element="label" class="error" for="file"/>
<c:if test="${not empty error}"><label class="error">${error}</label></c:if>
有沒有一種辦法,把槓桿BindingResult
處理異常的驗證錯誤以避免冗餘錯誤消息模板?
你知道,我只是完全駁回從我的想法是想法。捕捉DataAccessException時是否應該擔心?我記得在某個地方讀到這是你不應該接觸的東西,這就是爲什麼我以前沒有想過。 – Aetylus