簡體   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