简体   繁体   English

捕获Spring中PropertyEditors抛出的IllegalArgumentException

[英]Catching the IllegalArgumentException thrown by PropertyEditors in Spring

I have a PropertyEditor in order to translate ids into Persons , with it's setAsText (String text) as follows: 我有一个PropertyEditor以便将id转换为Persons ,它的setAsText(字符串文本)如下:

public void setAsText(String text) throws IllegalArgumentException {
    try {
        int id = Integer.parseInt(text);
        Person person = peopleService.get(id);
        this.setValue(person);
    }
    catch (NumberFormatException ex) {
        // ...
        throw new IllegalArgumentException("Not a number!: " + text);
    }
    catch (PersonNotFoundExcetion ex) {
        // ...
        throw new IllegalArgumentException("Impossible to get Person: " + text);
    }
}

And my PeopleController has a method as follows: 我的PeopleController具有如下方法:

@RequestMapping("/getPerson")
public void ver (@RequestParam Person person, Model model) {
    model.addAttribute (person);
    // ...
}

I want to catch the IllegalArgumentException in order to show a friendly message to the user, such as "Sorry, the Person you are looking for isn't here", but I don't know where to do that... 我想捕获IllegalArgumentException以便向用户显示友好消息,例如“对不起,您要查找的人不在这里”,但是我不知道该在哪里做...

Thanks! 谢谢!

General exception handling can be done in this way: 常规异常处理可以通过以下方式完成:

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleAllExceptions(Exception e) {
  return "redirect:/error.html"; /* use the correct view name */
}

More specfic you could use BindingResult 更具体地讲,您可以使用BindingResult

@RequestMapping(value = "/datedata", method = RequestMethod.POST)
public String create(
    @ModelAttribute("datedata") final DateData datedata,
    final BindingResult result) {

    if (result.hasErrors()) {
        return "datedata/create";
    } else {
        ...
        return "myView";
    }
 }

But I guess this works only for "Forms" (ModelAttribute) 但我想这仅适用于“ Forms”(ModelAttribute)

In my humble opinion it is not a good idea to let Spring handle validaten of user input by property editors. 以我的拙见,让Spring处理属性编辑器对用户输入的验证不是一个好主意。 I would strongly recommend to use the Form way: Build a command object with a STRING field an use a validator on it. 我强烈建议您使用Form方式:使用STRING字段构建命令对象,并在其上使用验证器。

The exception ought to be caught in the Controller. 异常应在控制器中捕获。 It should never leak out to the view and end user. 它永远不会泄漏给视图和最终用户。

If this is a web app, I'd recommend using the validation and binding API rather than PropertyEditor. 如果这是一个Web应用程序,我建议使用验证和绑定API而不是PropertyEditor。 That will allow you to return Errors that you can use to tell the UI what needs to be corrected. 这将使您返回可以用来告诉UI需要纠正的错误。

Your exception handling needs work. 您的异常处理需要工作。 I would not recommend catching an exception and doing nothing other than wrapping it and re-throwing. 我不建议捕获异常,除了将其包装并重新抛出外,什么也不做。 That's not handling anything or adding new information. 那什么也没做,也没有添加新信息。 It's actually less information as coded. 实际上,所编码的信息较少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM