Spring异常处理:org.springframework.beans.factory.CannotLoadBeanClassException

时间:2016-03-02 12:48:08

标签: java spring-mvc jar

我收到错误

  

抛出java.lang.ClassNotFoundException:   org.springframework.web.servlet.handler.SimpleMappingExceptionResolver

我在本地计算机上点击了网址:http://localhost:8080/SpringMvcExceptionHandling/student

Full Strack Trace是:

enter image description here

我已经检查了大部分问题/答案,但没有得到任何帮助来解决我的问题,请帮忙。

以下是我所做的: 我正在使用Jars进行Spring异常处理(Spring-3.1.2)。我不是在使用maven。

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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">


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

    <servlet-mapping>
        <servlet-name>SpringMvcExceptionHandling</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

SpringMvcExceptionHandling-servlet.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.exceptionhandling.mvc.controller" />

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

    <bean
        class="org.springframework.web.servlet.handler.
      SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="com.exceptionhandling.mvc.controller.SpringException">
                ExceptionPage
                </prop>
                <prop key="java.lang.Exception">error</prop>
            </props>
        </property>
        <property name="defaultErrorView" value="/error" />
    </bean>
</beans>

StudentController.java

package com.exceptionhandling.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.exceptionhandling.mvc.model.Student;


@Controller
public class StudentController {

    @RequestMapping(value = "/student")
    public ModelAndView student() {
        return new ModelAndView("student", "command", new Student());

    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    @ExceptionHandler({SpringException.class})
    public String addStudent(@ModelAttribute("SpringMvcExceptionHandling") Student student,
            ModelMap model) {

        if (student.getName().length() < 5) {
            throw new SpringException("Given name is too short");
        } else {
            model.addAttribute("name", student.getName());
        }

        if (student.getAge() < 10) {
            throw new SpringException("Given age is too low");
        } else {
            model.addAttribute("age", student.getAge());
        }
        model.addAttribute("id", student.getId());
        return "result";

    }
}

SpringException.java

package com.exceptionhandling.mvc.controller;

public class SpringException extends RuntimeException {

    private static final long serialVersionUID = 1L;
    private String exceptionMsg;

    public SpringException(String exceptionMsg) {
        this.exceptionMsg = exceptionMsg;
    }

    public String getExceptionMsg() {
        return exceptionMsg;
    }

    public void setExceptionMsg(String exceptionMsg) {
        this.exceptionMsg = exceptionMsg;
    }

}

我的项目结构看起来像

enter image description here

enter image description here

添加了两个外部罐子:

enter image description here

1 个答案:

答案 0 :(得分:0)

在您的SpringMvcExceptionHandling-servlet.xml文件中,您的bean类名称必须是完全限定的(中间没有空格)。

所以你的解决方案必须是:

 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">