本文介绍了如何从BindingResult获取控制器中的错误文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回JSON的控制器。它采用一种形式,通过弹簧注释验证自己。我可以从BindingResult获取FieldError列表,但它们不包含JSP将在标记中显示的文本。如何通过JSON发送错误文本?

I have an controller that returns JSON. It takes a form, which validates itself via spring annotations. I can get FieldError list from BindingResult, but they don't contain the text that a JSP would display in the tag. How can I get the error text to send back in JSON?

@RequestMapping(method = RequestMethod.POST)
public
@ResponseBody
JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) {

    if (result.hasErrors()) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        JSONResponse r = new JSONResponse();
        r.setStatus(JSONResponseStatus.ERROR);
        //HOW DO I GET ERROR MESSAGES OUT OF BindingResult??? 
    } else {
        JSONResponse r = new JSONResponse();
        r.setStatus(JSONResponseStatus.OK);
        return r;
    }

}

JSONREsponse类只是一个POJO

JSONREsponse class is just a POJO

public class JSONResponse implements Serializable {
    private JSONResponseStatus status;
    private String error;
    private Map<String,String> errors;
    private Map<String,Object> data;

...getters and setters...
}

调用BindingResult.getAllErrors()返回FieldError对象的数组,但它没有实际的错误。

Calling BindingResult.getAllErrors() returns an array of FieldError objects, but it doesn't have the actual errors.

推荐答案

免责声明:我仍然不使用Spring-MVC 3.0

Disclaimer: I still do not use Spring-MVC 3.0

但我认为Spring 2.5使用的相同方法可以满足您的需求

But i think the same approach used by Spring 2.5 can fullfil your needs

for (Object object : bindingResult.getAllErrors()) {
    if(object instanceof FieldError) {
        FieldError fieldError = (FieldError) object;

        System.out.println(fieldError.getCode());
    }

    if(object instanceof ObjectError) {
        ObjectError objectError = (ObjectError) object;

        System.out.println(objectError.getCode());
    }
}

我希望它对你有用

更新

如果您想获取资源包提供的消息,则需要已注册的messageSource实例(必须称为messageSource)

If you want to get the message provided by your resource bundle, you need a registered messageSource instance (It must be called messageSource)

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="ValidationMessages"/>
</bean>

在View中注入MessageSource实例

Inject your MessageSource instance inside your View

@Autowired
private MessageSource messageSource;

要获得您的信息,请执行以下操作

And to get your message, do as follows

for (Object object : bindingResult.getAllErrors()) {
    if(object instanceof FieldError) {
        FieldError fieldError = (FieldError) object;

        /**
          * Use null as second parameter if you do not use i18n (internationalization)
          */

        String message = messageSource.getMessage(fieldError, null);
    }
}

您的验证码应该是

/**
  * Use null as fourth parameter if you do not want a default message
  */
errors.rejectValue("<FIELD_NAME_GOES_HERE>", "answerform.questionId.invalid", new Object [] {"123"}, null);

这篇关于如何从BindingResult获取控制器中的错误文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:14