Search code examples
javaspringspring-mvcspring2.x

Spring : ClassNotFound exception


I am starting a new project in spring mvc, while I was trying to run hello world, it is throwing errors :

This is my dispatcher-servlet.xml mapping:

     <?xml version="1.0" encoding="UTF-8"?>
     <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean name="/welcome.html" 
        class="com.shaurya.FirstController" />

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

this is web.xml :

       <web-app id="WebApp_ID" version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

       <display-name>Spring Web MVC Application</display-name>
       <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>*.html</url-pattern>
   </servlet-mapping>
</web-app>

Controller code is here:

    package com.shaurya;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController

    public class FirstController extends AbstractController{
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse  response) throws Exception {
    String message="welcome to Spring";
    ModelAndView model= new ModelAndView("welcome");
    model.addObject("message", message);
     return model;
       }
     }

This is the view page for controller to render the welcome.jsp:

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
      <body>
        <a href="welcome.html">sdsd</a>
     </body>
    </html>

why there is an error, it appears something like this :

    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find       class [com.shaurya.FirstController] for bean with name'/welcome.html' enter code heredefined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.shaurya.FirstController.

Any suggestions what is missing here?


Solution

  • Can you try changing this code

    <bean name="/welcome.html" 
            class="com.shaurya.FirstController" />
    

    to this

    <bean name="/welcome" 
            class="com.shaurya.FirstController" />
    

    As the mapping for .html is already done in web.xml you probably shouldnt need to mention it again in bean mapping