servlet-class references to "org.springframework.web.servlet.DispatcherServlet" that does not implement interface javax.servlet.Servlet

StackOverflow https://stackoverflow.com/questions/21966070

  •  15-10-2022
  •  | 
  •  

문제

I am new to Spring and trying to deploy and run my first spring web application using Maven.For more details,please refer here(a question I posted yesterday):

Now,the problem I am having is: When I am trying the follwing 2 URL's,I am getting an HTTP Status 404 - Resource not available.

http://localhost:8080/CounterWebApp/welcome
http://localhost:8080/CounterWebApp/welcome/sandeep

Tomcat Server Console Warning message:

No mapping found for HTTP request URI [/CounterWebApp/welcome]
No mapping found for HTTP request URI [/CounterWebApp/welcome/sandeep]

This is the relevant portion of my web.xml:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

Here I am having a warning :

servlet-class references to "org.springframework.web.servlet.DispatcherServlet" that does not implement interface javax.servlet.Servlet

Controller class:

package com.sandeep.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class BaseController {

    @RequestMapping(value="/welcome", method = RequestMethod.GET)
    public String welcome(ModelMap model) {

        model.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()");

        //Spring uses InternalResourceViewResolver and return back index.jsp
        return "index";

    }

    @RequestMapping(value="/welcome/{name}", method = RequestMethod.GET)
    public String welcomeName(@PathVariable String name, ModelMap model) {

        model.addAttribute("message", "Maven Web Project + Spring 3 MVC - " + name);
        return "index";

    }

}

What is it that I am missing out? Please help.

Edit:

web.xml:

<servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

mvc-dispatcher-servlet.xml:

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

And I have this index.jsp inside /WEB-INF/pages/.

pom.xml(servlet api dependency)

<!--Servlet API Dependency-->
                <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                        <version>2.5</version>
                        <scope>provided</scope>
                </dependency>
도움이 되었습니까?

해결책

Solution:

The base-package value in mvc-dispatcher-servlet.xml was incorrect.

I had to correct it and then - build,deploy and run. :)

mvc-dispatcher-servlet.xml(final)

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

        <context:component-scan base-package="com.sandeep.controller" />

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

    </beans>

Here is the entire documentation :)

다른 팁

Change your URL Pattern from

<url-pattern>/</url-pattern>

To

<url-pattern>/*</url-pattern>

It should work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top