當前位置: 首頁>>代碼示例>>Java>>正文


Java FieldError.getField方法代碼示例

本文整理匯總了Java中org.springframework.validation.FieldError.getField方法的典型用法代碼示例。如果您正苦於以下問題:Java FieldError.getField方法的具體用法?Java FieldError.getField怎麽用?Java FieldError.getField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.validation.FieldError的用法示例。


在下文中一共展示了FieldError.getField方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: build

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
/**
 * 隻將第一個錯誤拋出異常
 * @param errResult
 * @return
 */
public static ValidationException build(BindingResult errResult) {

    FieldError fieldError = errResult.getFieldError();

    String code = fieldError.getCode();
    String field = fieldError.getField();
    String message = fieldError.getDefaultMessage();
    message = "`" + field + "` " + message;

    ErrorCode errorCode = ErrorCodeConfig.PARAM_INVAILD;
    switch (code) {
        case "NotNull":
            errorCode = ErrorCodeConfig.PARAM_NOTFOUND;
            break;
        case "Length":
            errorCode = ErrorCodeConfig.PARAM_INVAILD_LENGTH;
            break;
        default:
            break;
    }

    return new ValidationException(errorCode, message);
}
 
開發者ID:lordking,項目名稱:spring-rest-sample,代碼行數:29,代碼來源:ValidationExceptionUtils.java

示例2: escapeObjectError

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private <T extends ObjectError> T escapeObjectError(T source) {
	if (source == null) {
		return null;
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value,
				fieldError.isBindingFailure(), fieldError.getCodes(),
				fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(),
				HtmlUtils.htmlEscape(source.getDefaultMessage()));
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:23,代碼來源:EscapedErrors.java

示例3: addEntry

import org.springframework.validation.FieldError; //導入方法依賴的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";
}
 
開發者ID:imaTowan,項目名稱:Towan,代碼行數:27,代碼來源:ForumController.java

示例4: addTopic

import org.springframework.validation.FieldError; //導入方法依賴的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";
}
 
開發者ID:imaTowan,項目名稱:Towan,代碼行數:17,代碼來源:ForumController.java

示例5: handleException

import org.springframework.validation.FieldError; //導入方法依賴的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);
}
 
開發者ID:evoila,項目名稱:cfsummiteu2017,代碼行數:11,代碼來源:BaseController.java

示例6: addSubforum

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
@RequestMapping(value = "/addSubforum", method = RequestMethod.POST)
public String addSubforum(@Valid @ModelAttribute SubforumModel newSubforumModel, 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/subforum";
	}

	subforumRepository.save(newSubforumModel);
	return "forum/subforum";
	}
 
開發者ID:imaTowan,項目名稱:Towan,代碼行數:16,代碼來源:ForumController.java

示例7: FieldErrorDto

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
public FieldErrorDto(FieldError fieldError) {
  this.fieldName = fieldError.getField();
  this.message = fieldError.getDefaultMessage();
}
 
開發者ID:mental-party,項目名稱:meparty,代碼行數:5,代碼來源:FieldErrorDto.java

示例8: handle

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity handle(MethodArgumentNotValidException e) {
    FieldError error = e.getBindingResult().getFieldError();
    return new ResponseEntity(400, error.getField()+": "+error.getDefaultMessage(), null);
}
 
開發者ID:Eagle-OJ,項目名稱:eagle-oj-api,代碼行數:7,代碼來源:GlobalExceptionController.java

示例9: RestFieldError

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
public RestFieldError(FieldError error) {
    this.field = error.getField();
    this.rejectedValue = error.getRejectedValue();
    this.message = error.getDefaultMessage();
}
 
開發者ID:MasonQAQ,項目名稱:WeatherSystem,代碼行數:6,代碼來源:RestFieldError.java

示例10: ValidationErrorResource

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
public ValidationErrorResource(FieldError fieldError) {
    this.property = fieldError.getField();
    this.message = fieldError.getDefaultMessage();
    this.invalidValue = String.valueOf(fieldError.getRejectedValue());
}
 
開發者ID:tsypuk,項目名稱:springrestdoc,代碼行數:6,代碼來源:ValidationErrorResource.java

示例11: processFieldError

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
private Map<String, Object> processFieldError(Map<String, Object> data, Class<?> targetClass, List<FieldError> errors) throws NoSuchFieldException {
    for (FieldError error : errors) {
        String field = error.getField();
        String key = field + error.getCode();
        MsgCode msgCode = null;
        if (null == errorFields.get(key)) {
            String[] pfields = field.split("\\.");
            //判斷是否為雙層對象並進行切割獲取字段對象的字段並抓取code
            if (pfields.length >= 2) {
                String temp = pfields[0];
                if (temp.contains("[")) {
                    temp = temp.substring(0, temp.indexOf("["));
                }
                Field declaredField = targetClass.getDeclaredField(temp);
                Class clazz = declaredField.getType();
                if (clazz.isAssignableFrom(List.class) || clazz.isAssignableFrom(Map.class)) {
                    Type genType = declaredField.getGenericType();
                    if (!(genType instanceof ParameterizedType)) {
                        clazz = Object.class;
                    } else {
                        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
                        try {
                            clazz = (Class) params[0];
                        } catch (Exception e) {
                            try {
                                clazz = (Class) ((ParameterizedType) params[0]).getRawType();
                            } catch (Exception e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                }
                msgCode = clazz.getDeclaredField(pfields[1]).getAnnotation(MsgCode.class);
            } else {
                msgCode = targetClass.getDeclaredField(field).getAnnotation(MsgCode.class);
            }
            errorFields.put(key, msgCode);
        } else {
            msgCode = errorFields.get(key);
        }

        if (null != msgCode) {
            data.put(CODE_KEY, errorCode.getCode(msgCode.value()));
        }
        //根據code 在error_code.yml取值,如果未取到值則去默認的error.getDefaultMessage();
        String message = errorCode.getMessage(msgCode != null ? msgCode.value() : null);
        if (null == message) {
            message = error.getDefaultMessage();
        }
        data.put(MESSAGE_KEY, message);
        return data;
    }
    data.put(CODE_KEY, 1000);
    return data;
}
 
開發者ID:egzosn,項目名稱:spring-jdbc-orm,代碼行數:56,代碼來源:BaseController.java

示例12: UIErrorDetail

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
UIErrorDetail(FieldError error) {
    rejectedValue = error.getRejectedValue();
    path = error.getObjectName() + '.' + error.getField();
    message = "Invalid value for field '" + error.getField() + "': " + error.getDefaultMessage();
}
 
開發者ID:luxons,項目名稱:seven-wonders,代碼行數:6,代碼來源:UIErrorDetail.java

示例13: FormErrorDto

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
public FormErrorDto(FieldError fieldError) {
    this.field = fieldError.getField();
    this.code = fieldError.getCode();
    this.message = fieldError.getDefaultMessage();
    this.rejectedValue = fieldError.getRejectedValue();
}
 
開發者ID:abixen,項目名稱:abixen-platform,代碼行數:7,代碼來源:FormErrorDto.java

示例14: loadListErrors

import org.springframework.validation.FieldError; //導入方法依賴的package包/類
/**
 * Iterate over list items errors and load it on allErrorsMessages map.
 * <p/>
 * Delegates on {@link #loadObjectError(FieldError, String, Map)}
 * 
 * @param fieldErrors
 * @param allErrorsMessages
 */
@SuppressWarnings("unchecked")
private void loadListErrors(List<FieldError> fieldErrors, Map<String, Object> allErrorsMessages) {

  // Get prefix to unwrapping list:
  // "list[0].employedSince"
  String fieldNamePath = fieldErrors.get(0).getField();
  String prefix = "";

  if (fieldNamePath != null && fieldNamePath.contains("[")) {
    // "list"
    prefix = substringBefore(fieldNamePath, "[");
  }

  String index = "";
  Map<String, Object> currentErrors;

  // Iterate over errors
  for (FieldError error : fieldErrors) {

    fieldNamePath = error.getField();

    if (fieldNamePath != null && fieldNamePath.contains("]")) {

      // get property path without list prefix
      // "[0].employedSince"
      fieldNamePath = substringAfter(error.getField(), prefix);

      // Get item's index:
      // "[0].employedSince"
      index = substringBefore(substringAfter(fieldNamePath, "["), "]");

      // Remove index definition from field path
      // "employedSince"
      fieldNamePath = substringAfter(fieldNamePath, ".");
    }

    // Check if this item already has errors registered
    currentErrors = (Map<String, Object>) allErrorsMessages.get(index);
    if (currentErrors == null) {
      // No errors registered: create map to contain this error
      currentErrors = new HashMap<String, Object>();
      allErrorsMessages.put(index, currentErrors);
    }

    // Load error on item's map
    loadObjectError(error, fieldNamePath, currentErrors);
  }
}
 
開發者ID:DISID,項目名稱:springlets,代碼行數:57,代碼來源:BindingResultSerializer.java


注:本文中的org.springframework.validation.FieldError.getField方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。