Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No mapping found for HTTP request with URI.... in DispatcherServlet with name [duplicate]

I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.

Here is the code: web.xml:

   <display-name>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>/</url-pattern>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.xml:

<context:component-scan base-package="com.mycompany.elso" />
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>   

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

myController:

public class myController {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message); 
    }
}

Web Pages/index.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series</title>
</head>
<body>
    <a href="hello.html">Say Hello</a>
</body>
</html>

Web Pages/WEB-INF/jsp/hello.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
    ${message}
</body>
</html>

So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:

No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'

I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?

like image 712
erik.c Avatar asked Sep 08 '13 12:09

erik.c


5 Answers

Add

  <mvc:default-servlet-handler/>

to spring-servlet.xml

like image 158
Harry Avatar answered Nov 10 '22 19:11

Harry


You could try and add an @Controller annotation on top of your myController Class and try the following url /<webappname>/my/hello.html. This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping prepends /my to each RequestMapping in the myController class.

like image 44
Nils Avatar answered Nov 10 '22 20:11

Nils


If you are using

<mvc:annotation-driven/> 

make sure your spring-servlet.xml has correct

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

Basically, you need to include all the packages where you have used the annotation in your java code.

Also, please ensure you do not have duplication of component-scan (for a discovery of beans). If your config XML already contains the element, then any of your Controller classes that are annotated with @ComponentScan(basePackages=... needs to be stripped of the said annotation.

like image 30
ssk Avatar answered Nov 10 '22 18:11

ssk


I solved my issue with : Java Build Path -> JRE system library - > Edit -> Alternate JRE -> -> Finish

As it was configured to JDK folder so it was giving Exception

like image 15
acg Avatar answered Nov 10 '22 20:11

acg


Make sure

<mvc:annotation-driven/>
<context:component-scan base-package="com.hireartists.web.controllers"/>

points to proper package that contains controllers.

like image 12
prayagupa Avatar answered Nov 10 '22 19:11

prayagupa