繁体   English   中英

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

[英]Spring Exception Handling : org.springframework.beans.factory.CannotLoadBeanClassException

我收到错误消息

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

我在本地计算机上访问URL: http:// localhost:8080 / SpringMvcExceptionHandling / student

完整的跟踪记录是:

在此处输入图片说明

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

这是我所做的事情:我正在使用Jars(Spring-3.1.2)进行Spring异常处理。 我不使用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;
    }

}

我的项目结构看起来像

在此处输入图片说明

罐子

在此处输入图片说明

添加了两个外部Jar:

在此处输入图片说明

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

因此,您的解决方案必须是:

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

暂无
暂无

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

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