Spring MVC - No mapping found for HTTP request with URI [duplicate]

非 Y 不嫁゛ 提交于 2019-12-08 04:09:28

问题


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>


来源:https://stackoverflow.com/questions/13247921/spring-mvc-no-mapping-found-for-http-request-with-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!