java – 没有使用URI发现HTTP请求的映射Spring MVC

参见英文答案 > Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?3
这是我的Web.xml

dispatcherServlet的

org.springframework.web.servlet.dispatcherServlet


contextConfigLocation的

/WEB-INF/spring/servlet-context.xml


1

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

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

我的servlet-context.xml

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

最后是Handler课.它在com.springexample.controller.impl下

@Controller
public class IndexControllerImpl implements IndexController {

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

        return "index";
    }
}

但是去本地主机: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 '

这是我的项目结构

解决方法

使用web.xml配置他们的方式你有问题,特别是:
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

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

因此,当您的控制器返回指向.jsp的视图,而不是允许服务器容器服务该请求时,dispatcherServlet将跳转并开始寻找可以为该请求提供服务的控制器,但它没有找到任何因此404.

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

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

注意缺少*.这告诉容器,任何没有路径信息的请求(在最后没有.xxx的urls)应该发送到dispatcherServlet.使用此配置,当接收到xxx.jsp请求时,未查询dispatcherServlet,并且您的servlet容器的认servlet将为请求提供服务并按预期提供jsp.

希望这有帮助,我意识到你以前的注释说明问题已经解决了,但解决方案不能只是将Request = RequestMethod.GET添加到RequestMethod中.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...