@ControllerAdvice不处理异常

时间:2014-07-04 22:42:21

标签: java spring spring-mvc

我已经浏览了所有相关的帖子,但我的@ControllerAdvice似乎没有处理从Controller类抛出的自定义异常。但@Controller类中的@ExceptionHandler确实处理了异常。我没有想法是什么错误。

的web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5">    
<display-name>Archetype Created Web Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

调度-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:component-scan base-package="com.test.controller,com.test.exception"/>
 <mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean> 

@ControllerAdvice Class:

package com.test.controller;
import com.test.exception.EmployeeException;

@EnableWebMvc
@ControllerAdvice         
public class EmployeeExceptionController {

@ExceptionHandler(EmployeeException.class)
public ModelAndView employeeNotFound(EmployeeException ex){

    String msg="employee with the "+ex.getId()+" not found";
    ModelAndView mv=new ModelAndView("helloWorld");
    mv.addObject("message",msg);
    mv.addObject("name","boss");
    return mv;
   }

}

@ControllerAdvice与控制器位于同一个包中。

0 个答案:

没有答案