I made a simple Spring MVC application in Eclipse. It start to give problems with annotations

Satyendra Singh :

When I access First_Spring_MVC/welcome I get the following error:

org.springframework.web.servlet.PageNotFound noHandlerFound

WARNING: No mapping found for HTTP request with URI [/First_Spring_MVC/welcome] in DispatcherServlet with name 'spring-dispatcher'

Here's my folder structure:

enter image description here

Here's my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>First_Spring_MVC</display-name>
  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Here's spring-dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

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

<context:component-scan base-package="com.webapp.helloController" /> 

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

And this is the java file I'm using for controller class.

@Controller
public class HelloController {

    @RequestMapping("/welcome")
    public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("HelloPage");
        model.addObject("welcomeMessage", "hello world");

        return model;
    }
}
Nicholas K :

You are scanning the wrong package. Change :

<context:component-scan base-package="com.webapp.helloController" />

to

<context:component-scan base-package="com.app.helloController" /> 

Also make sure you are hitting the right url, in your case :

http://localhost:{your_port_num}/First_Spring_MVC/welcome

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152640&siteId=1