1

这已经在谷歌领域被问过几次,但我似乎无法将这些解决方案应用于我的情况。我的 J2EE 应用程序使用 Spring,而我之前使用 SimpleMappingExceptionResolver 来拦截异常,以便为我的用户显示一个友好的错误页面。我团队中的一个人有他自己的包,并使用扩展的异常类作为向他的用户传达验证错误的工具。所以现在每次用户输入无效数据,都会触发一个异常页面。他拒绝更改他的代码(尽管我认为他应该这样做),所以现在我不得不对特定于包或控制器的事情进行错误处理。

我试图利用@ExceptionHandler注释来做到这一点,但它似乎没有被解雇。我究竟做错了什么?我期待调用dmapproval实质上返回applicationError视图。

这是我的控制器:

@Controller
public class ExecutiveApprovalController {

    /*omitted stuff*/   

    @ExceptionHandler(Exception.class)
    public String routToErrorHandler(Exception anExc) {
        return "applicationError";
    }

    @RequestMapping(value = "/dmapproval", method = RequestMethod.GET)
    public String dmApproval(Model model, HttpServletRequest request) throws Exception {    

        throw new RuntimeException(); // just for testing exception logic
    }

    /*omitted stuff here, too*/
}    

谢谢你的帮助!!

4

2 回答 2

1

Your snippet looks fine; the only difference between your code and something that I have working is that I annotate the exception handler method like this:

@ExceptionHandler(Throwable.class)

As an aside; this is far less elegant than using SimpleMappingExceptionResolver (which it sounds like you already know!). I'd ask you rogue team-mate to reconsider his validation approach and use Exceptions only in exceptional circumstances ;)

于 2013-08-14T16:42:34.637 回答
0

您是否在 servlet-context.xml 中添加了此配置

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">

于 2013-08-15T02:47:17.667 回答