简体   繁体   中英

Spring Exception Handling : org.springframework.beans.factory.CannotLoadBeanClassException

I am getting error :

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

I am hitting URL : http://localhost:8080/SpringMvcExceptionHandling/student on my local machine

Full Strack Trace is :

在此处输入图片说明

I have already checked most of the question/answer but not getting any help to resolve my issue,Please help.

Here is what I have done : I am working on Spring Exception Handling with Jars(Spring-3.1.2). I am not using maven.

web.xml file

<?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 file

    <?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;
    }

}

My Project Structure Looks Like

在此处输入图片说明

Jars

在此处输入图片说明

Added Two External Jars :

在此处输入图片说明

In your SpringMvcExceptionHandling-servlet.xml file, your bean class name must be fully qualified (with no spaces in between).

So your solution must be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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