本文整理匯總了Java中org.springframework.validation.BindingResult.getFieldErrors方法的典型用法代碼示例。如果您正苦於以下問題:Java BindingResult.getFieldErrors方法的具體用法?Java BindingResult.getFieldErrors怎麽用?Java BindingResult.getFieldErrors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.validation.BindingResult
的用法示例。
在下文中一共展示了BindingResult.getFieldErrors方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: processHandler
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
/**
* Rest handler for validation errors.
* @param ex handled exception
* @return rest result
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<?> processHandler(MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
List<FieldErrorDto> fieldErrorDtos = fieldErrors.stream()
.map(FieldErrorDto::new)
.collect(Collectors.toList());
ValidationResultDto validationResultDto = new ValidationResultDto();
validationResultDto.setFieldErrors(fieldErrorDtos);
LOGGER.error("VALIDATION ERROR: " + ex.getMessage());
return ResponseEntity.badRequest().body(validationResultDto);
}
示例2: saveEmp
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
/**
* 員工保存
* @param employee
* @return
* 1.支持JSR303校驗
* 2.導入Hibernate-Validator
*/
@RequestMapping(value="emp",method=RequestMethod.POST) //定義為POST請求
@ResponseBody
public Msg saveEmp(@Valid Employee employee,BindingResult result){
if(result.hasErrors()){
Map<String,Object> map= new HashMap<>();
//校驗失敗,應該返回失敗,在模態框中顯示校驗失敗的信息
List<FieldError> errors = result.getFieldErrors(); //提取出錯誤信息
for(FieldError fieldError : errors){
System.out.println("錯誤的字段名:"+ fieldError.getField());
System.out.println("錯誤信息:"+fieldError.getDefaultMessage());
map.put(fieldError.getField(), fieldError.getDefaultMessage());
}
return Msg.fail().add("errorFields", map);
}else{
employeeService.saveEmp(employee);
return Msg.success();
}
}
示例3: addTopic
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
@RequestMapping(value = "/addTopic", method = RequestMethod.POST)
public String addTopic(@Valid @ModelAttribute ForumModel newForumModel, BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
String errorMessage = "";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMessage += fieldError.getField() + " is invalid<br>";
}
model.addAttribute("errorMessage", errorMessage);
return "/forum";
}
forumRepository.save(newForumModel);
return "/forum";
}
示例4: addEntry
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
@RequestMapping(value = "/addEntry", method = RequestMethod.POST)
public String addEntry(@Valid @ModelAttribute EntryModel newEntryModel, BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
String errorMessage = "";
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMessage += fieldError.getField() + " is invalid<br>";
}
model.addAttribute("errorMessage", errorMessage);
return "forward:/entry";
}
//if (newEntryModel != null) {
//model.addAttribute("errorMessage", "Entry already exists!<br>");
//} else {
newEntryModel.setDate(new Timestamp(System.currentTimeMillis()));
UserModel user = null;
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<UserModel> userList = userRepository.findByUsername(auth.getName());
user = userList.get(0);
newEntryModel.setUser(user);
newEntryModel.setSubforum(null);
entryRepository.save(newEntryModel);
return "forum/entry";
}
示例5: addList
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
/**
* adds new shopping list for the current user
*
* @param newList
* ShoppingListDTO with basic information about new list (must
* include list name)
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
public String addList(Principal principal, @Valid @ModelAttribute("newList") ShoppingListDTO newList, BindingResult binding, RedirectAttributes attr, HttpSession session) {
LOGGER.debug("addList() method of MvcController called for user: {}", principal.getName());
if(!binding.hasErrors())
repositoryService.addShoppingListToUserByName(principal.getName(), newList.getListName());
else {
attr.addFlashAttribute("org.springframework.validation.BindingResult.newList", binding);
attr.addFlashAttribute("newList", newList);
for(FieldError ferr:binding.getFieldErrors()) {
LOGGER.info("addList(): field error: " + ferr.getDefaultMessage());
}
}
return "redirect:/list";
}
示例6: processValidationError
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
ErrorVM dto = new ErrorVM(ErrorConstants.ERR_VALIDATION);
for (FieldError fieldError : fieldErrors) {
dto.add(fieldError.getObjectName(), fieldError.getField(), fieldError.getCode());
}
return dto;
}
示例7: handleException
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorMessage> handleException(MethodArgumentNotValidException ex,
HttpServletResponse response) {
BindingResult result = ex.getBindingResult();
String message = "Missing required fields:";
for (FieldError error: result.getFieldErrors()) {
message += " " + error.getField();
}
return processErrorResponse(message, HttpStatus.UNPROCESSABLE_ENTITY);
}
示例8: processValidationError
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
示例9: processValidationError
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
示例10: processValidationError
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
/**
* Method binding issues (raised by Spring framework) - mapped to BAD_REQUEST.
*
* @param ex
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestErrorResponse processValidationError(MethodArgumentNotValidException ex) {
final BindingResult result = ex.getBindingResult();
final Map<String, Object> globalErrorsMap = new LinkedHashMap<>();
final List<ObjectError> globalErrors = result.getGlobalErrors();
for (ObjectError objectError : globalErrors) {
globalErrorsMap.put(objectError.getObjectName(), objectError.getDefaultMessage());
}
final List<FieldError> fieldErrors = result.getFieldErrors();
final Map<String, Object> fieldErrorsMap = new LinkedHashMap<>();
for (FieldError fieldError : fieldErrors) {
fieldErrorsMap.put(fieldError.getObjectName() + '.' + fieldError.getField(),
fieldError.getDefaultMessage());
}
final Map<String, Object> additionalDetails = new LinkedHashMap<>();
if (!globalErrorsMap.isEmpty()) {
additionalDetails.put("global-errors", globalErrorsMap);
}
if (!fieldErrorsMap.isEmpty()) {
additionalDetails.put("field-errors", fieldErrorsMap);
}
final RestErrorResponse errorResponse =
new RestErrorResponse(HttpStatus.BAD_REQUEST.value(), "Failed to validate request");
if (!additionalDetails.isEmpty()) {
errorResponse.setAdditionalDetails(additionalDetails);
}
return errorResponse;
}
示例11: assertRequest
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
private void assertRequest(BindingResult result) throws DtoValidationFailedException {
if (result.hasErrors()) {
throw new DtoValidationFailedException("Validation failed!", result.getFieldErrors());
}
}
示例12: assertRequest
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
private void assertRequest(BindingResult bindingResult) throws DtoValidationFailedException {
if (bindingResult.hasErrors()) {
throw new DtoValidationFailedException("Validation failed!", bindingResult.getFieldErrors());
}
}
示例13: assertEntityValidity
import org.springframework.validation.BindingResult; //導入方法依賴的package包/類
private void assertEntityValidity(BindingResult bindingResult) throws DtoValidationFailedException {
if (bindingResult.hasErrors()) {
throw new DtoValidationFailedException("Validation failed!", bindingResult.getFieldErrors());
}
}