将错误从服务层传递回视图 [英] Passing errors back to the view from the service layer

查看:107
本文介绍了将错误从服务层传递回视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我查看了Spring 3的 @ExceptionHandler 注释,并将其与下面的选项1 相结合是一个非常干净的解决方案。

I have looked into Spring 3's @ExceptionHandler annotation and combining this with Option 1 below looks to be a pretty clean solution.

参见 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers

我还发现这是一个很好的阅读: http://blog.decaresystems.ie/index.php/2006/04/07/difficult -choices-in-handling-exceptions-in-enterprise-java-applications /

I also found this to be a good read: http://blog.decaresystems.ie/index.php/2006/04/07/difficult-choices-in-handling-exceptions-in-enterprise-java-applications/

我去过现在使用Spring MVC框架进行开发一段时间了但是我正在努力想出一个'好'的方法来传递在ser中引发的错误副层回到JSP。

I have been developing using the Spring MVC framework for some time now however I am struggling to come up with a 'nice' way to pass errors that are raised in the service layer back to the JSP.

基本上,我不认为业务逻辑(超出此字段是强制性的)应该在Validators中,尤其是任何逻辑这需要访问数据库。所以,我一直在做的是在服务层中放置更复杂的验证和业务逻辑。

Basically, I don't believe that business logic (beyond "this field is mandatory") should be in the Validators, especially any logic that requires access to the DB. So, what I have been doing is placing further, more complicated validation and business logic in the service layer.

例如,假设我有一个允许用户的页面买书。他们在JSP上单击购买,控制器调用服务使其全部发生......现在,如果服务看到他们没有足够的资金会发生什么 - 我如何将此消息传回JSP,这样一个不错的小小可以向用户显示资金不足消息?我考虑过两种方式而且我不确定哪种方法是正确的...

For example, lets say I have a page that allows a user to buy a Book. They click "Purchase" on the JSP and the controller calls the service to make it all happen... Now, what happens if the service sees that they have insufficient funds - how do I get this message back to the JSP so a nice little "Insufficient funds" message can be displayed to the user? I have considered two ways and I'm not sure which is correct...

选项1:例外

我认为第一种方法是在服务层中引发异常,将其捕获到控制器中并向BindingResult添加消息。

The first way I thought was to raise an exception in the service layer, trap it in the controller and add a message to the BindingResult.

服务:

public void pay(Book book) throws InsufficientFundsException {
    // Some logic goes here, which ends up throwing the above exception
}

控制器:

public ModelAndView(@ModelAttribute("book") Book book, BindingResult errors) {
    try {
        pay(book);
    } catch (InsufficientFundsException ex) {
        errors.reject("insufficient.funds");
    }
    return new ModelAndView(blahblahblah);
}

选项2:将BindingResult传递给服务层

第二种方法是将BindingResult对象传递给服务层并针对它提出进一步的错误。

The second way was to pass the BindingResult object to the service layer and raise further errors against it.

服务:

public void pay(Book book, BindingResult errors) {
    // User has insufficient funds, so...
    errors.reject("insufficient.funds);
}

I可以看到这两种方式的问题。选项1感觉很尴尬,因为我不仅要捕获异常,然后我必须将错误添加到绑定结果,所以感觉我做了两次相同的事情。和选项2似乎将服务层绑定到控制器太紧。

I can see problems with both of these ways. Option 1 feels awkward because not only do I have to catch the exception, I then have to add the error to the binding result so it feels like I'm doing the same thing twice. And Option 2 seems to bind the service layer too tightly to the controller.

最后,我意识到有 SimpleMappingExceptionResolver 可以与选项1一起使用,但我不确定它是多么合适(也许我没有看到一个合适的例子吗?)。在上面的例子中,我们只是说为了参数的缘故,我希望用户返回到原始表单,表单上方有一个红色错误,而不是重定向到一个完全不同的页面。当你想要在引发某个异常时将用户重定向到标准错误页面时,SimpleMappingExceptionResolver似乎很有用(这不是我想知道的怎么做)。

Finally, I realise there is the SimpleMappingExceptionResolver that could be used in conjunction with Option 1, but I'm not sure how appropriate it is (perhaps I have not seen a proper example?). In the above example, lets just say for argument's sake that I'd like the user returned to the original form with a red error above the form, not redirected to an entirely different page. The SimpleMappingExceptionResolver seems to me to be useful when you want to redirect a user to a standard error page when a certain exception is raised (which is not quite what I want to know how to do).

推荐答案

Java使用异常来自然处理这类事情。最后,它通常会简化您的逻辑,并通过忘记检查某些内容是否有错误来减少出错的可能性。您还可以将错误逻辑从代码的主流中移出。

Java uses exceptions to naturally handle this kind of thing. In the end it generally simplifies your logic and reduces the chance of making a mistake by forgetting to check that something had an error. You are also able to move error logic out of the main flow of the code.

我不明白为什么您提出的案例不同于我提出的任何其他案例将使用异常处理来处理错误。

I don't see why the case you present is different from any other case where I would use exception handling to deal with errors.

这篇关于将错误从服务层传递回视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆