1
votes

I am new to spring, I am trying to create simple spring MVC application. But unfortunate I am getting error Error "creating bean with name 'HandlerMapping' defined in ServletContext resource".

Please find my files below and correct me where I am going wrong:

Web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstSpringMVC</display-name>

 <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spring-dispatcher-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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.springframework.samples.petclinic.web"/>


    <!-- Helper controller class -->
    <bean id="HandlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"
    ></bean>

    <!-- Req. process Controller class -->
    <bean name="/Welcome.html" class="C:/Users/Vijay Mekala/Desktop/SAP/Java Spring/FirstSpringMVC/src/com/vijay/hellocontroller/HelloController"></bean>

    <!-- View Resolver class -->
    <bean id="ViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceviewResolver"
    >
        <property name="prefix">
            <value>/WEB-INF</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

- HelloController

package com.vijay.hellocontroller;

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 HelloController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ModelAndView modelandview = new ModelAndView("Hellopage");
        modelandview.addObject("welcomemessage", "Welcome to MVC 1st Application");

        return modelandview;

//      return null;
    }

}

HelloPage.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>First MVC App OM</title>
</head>


<body>

<h2>
${welcomemessage}
</h2>
</body>
</html>

Few more details:

I am trying to create my 1st Spring MVC application without annotations. So herewith my URL request 'http://localhost:8080/FirstSpringMVC/Welcome.html' which invokes my code.

Herewith my list of errors for reference :

Root Cause -->

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'HandlerMapping' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [/FirstSpringMVC/src/HelloController/HelloController] for bean with name '/Welcome.html' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: /FirstSpringMVC/src/HelloController/HelloController Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [/FirstSpringMVC/src/HelloController/HelloController] for bean with name '/Welcome.html' defined in ServletContext resource

Root Cause ->

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [/FirstSpringMVC/src/HelloController/HelloController] for bean with name '/Welcome.html' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: /FirstSpringMVC/src/HelloController/HelloController org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385)

Root Cause -->

java.lang.ClassNotFoundException: /FirstSpringMVC/src/HelloController/HelloController

2

2 Answers

0
votes

There are couple of things to be corrected in your code.

1) Component scan base package should be changed -

<context:component-scan base-package="com.vijay.hellocontroller"/>

2)Bean creation should be corrected

<bean id="someid" class="com.vijay.hellocontroller.HelloController"></bean>

3) You man annotate your controller as below

@Controller
public class HelloController extends AbstractController
0
votes

You have given the absolute path for Mapping to your controller class:

<bean name="/Welcome.html" class="C:/Users/Vijay Mekala/Desktop/SAP/Java Spring/FirstSpringMVC/src/com/vijay/hellocontroller/HelloController"></bean> 

Try instead, to give the relative path as per your package:

<bean name="/Welcome.html" class="com.vijay.hellocontroller.HelloController"></bean>

And also replace this code

<?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"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base- 
package="org.springframework.samples.petclinic.web"/>

with the following

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

i think no need to mention this

  <context:component-scan base- 
package="org.springframework.samples.petclinic.web"/>

if your replace it by which i was given above