没有找到带有URI Spring MVC的HTTP请求的映射

use*_*206 13 java model-view-controller spring spring-mvc

这是我的Web.xml

dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring/servlet-context.xml 1

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

我的servlet-context.xml

<context:component-scan base-package="com.springexample.controller.impl" />
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
Run Code Online (Sandbox Code Playgroud)

最后是Handler类.它位于com.springexample.controller.impl下

@Controller
public class IndexControllerImpl implements IndexController {

    @RequestMapping("/")
    public String index() {

        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

但是要去 localhost:8080/projectname/

它返回404错误.

Jul 27, 2013 8:18:31 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/tasklist/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcherServlet'
Jul 27, 2013 8:18:37 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/tasklist/index] in DispatcherServlet with name '
Run Code Online (Sandbox Code Playgroud)

这是我的项目结构

项目结构

Aks*_*hay 40

通过配置web.xml,您可以了解问题,特别是:

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

对您的网络应用程序发出的所有请求都将被定向到DispatcherServlet.这包括像请求/tasklist/,/tasklist/some-thing.html,/tasklist/WEB-INF/views/index.jsp.

因此,当你的控制器返回一个指向a的视图.jsp,而不是让你的服务器容器为请求提供服务时,DispatcherServlet跳转并开始寻找可以为这个请求提供服务的控制器,它找不到任何一个,因此404.

最简单的解决方法是让servlet url映射如下:

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

注意缺少*.这告诉容器任何没有其中的请求path info(最后没有.xxx的URL)应该被发送到DispatcherServlet.使用此配置,当xxx.jsp收到请求时,DispatcherServlet不会查询请求,并且您的servlet容器的默认servlet将为请求提供服务并按预期显示jsp.

希望这有帮助,我意识到你之前的评论说明问题已经解决,但解决方案不能只是添加method=RequestMethod.GETRequestMethod.