本文整理汇总了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);
}
示例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()));
}
}
示例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";
}
示例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";
}
示例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);
}
示例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";
}
示例7: FieldErrorDto
import org.springframework.validation.FieldError; //导入方法依赖的package包/类
public FieldErrorDto(FieldError fieldError) {
this.fieldName = fieldError.getField();
this.message = fieldError.getDefaultMessage();
}
示例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);
}
示例9: RestFieldError
import org.springframework.validation.FieldError; //导入方法依赖的package包/类
public RestFieldError(FieldError error) {
this.field = error.getField();
this.rejectedValue = error.getRejectedValue();
this.message = error.getDefaultMessage();
}
示例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());
}
示例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;
}
示例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();
}
示例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();
}
示例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);
}
}