Spring MVC - No mapping found for HTTP request wit

2019-05-29 21:14发布

问题:

This question already has an answer here:

  • Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”? 6 answers

I'm aware that there are loads of questions on the topic but none of the solutions i found here worked for me. I'm using Spring with Jetty 6 so i don't have a web.xml file. The mapping for the spring dispatcher servlet is set to "/" in jetty's config

dispatcher:

<bean class="org.mortbay.jetty.servlet.ServletHolder">
    <property name="name" value="spring" />
    <property name="servlet">
        <bean class="org.springframework.web.servlet.DispatcherServlet" />
    </property>
    <property name="initParameters">
        <map>
            <entry key="contextConfigLocation" value="classpath:com/project/config/spring-servlet.xml" />
        </map>
    </property>
</bean>

... mapping:

<bean class="org.mortbay.jetty.servlet.ServletMapping">
    <property name="servletName" value="spring"></property>
    <property name="pathSpec" value="/"></property>
</bean>

The spring-servlet.xml looks like this:

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


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

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

And i have a simple controller called HelloController:

@Controller
public class HelloController {

    @RequestMapping(method = RequestMethod.GET, value="/welcome")
    public String sayHello(ModelMap model){
    model.addAttribute("message", "Spring 3 MVC Hello World");
    return "hello";
}

}

Reading the logs it seem to work but i get the following error:

No mapping found for HTTP request with URI [/WEB-INF/pages/hello.jsp] in DispatcherServlet with name 'spring'

which i don't understand. it maps the "/welcome" to /WEB-INF/pages/hello.jsp but it still says page cannot be found, which is just there where it seems to look for it. I added the WEB-INF folder to the classpath but it's still the same. Do you have any idea why's that?

回答1:

Are you sure the package name is correct in this?

<context:component-scan base-package="com.project.web" />


回答2:

The request mapping path in the controller is relative to your http://your-domain/your-app/. If your app name is welcome use url http://localhost:25001/welcome/welcome or change the requestmapping to @RequestMapping(method = RequestMethod.GET, value="/") so you can use url http://localhost:25001/welcome



回答3:

Is your hello.jsp directly under WEB-INF/pages? Can you change the Dispatcher Servlet mapping to this and try

<property name="pathSpec" value="*.html"></property>