【问题标题】:No mapping found for http request URI. Handler not found未找到 http 请求 URI 的映射。未找到处理程序
【发布时间】:2017-06-12 09:07:14
【问题描述】:

我知道有很多类似的问题。我已附上所有代码,但出现错误 404

index.jsp

 <html>
       <body>


         <div align="center">
          <h2> Addition </h2>

           <form action="Add" >

          N1= <input type="text" name="t1"><br><br>
          N2= <input type="text" name="t2"><br><br>
          <input type="submit">

    </form>

    </div>
  </body>
 </html>

AddController.java

package com.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller      
public class AddController {

    @RequestMapping("/Add")
    public String add(){
        return("display.jsp");
    }
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>SpringMVCTutorial</display-name>

    <servlet>
        <servlet-name>MVC1</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MVC1</servlet-name>
        <url-pattern>/</url-pattern>

    </servlet-mapping>

</web-app>

MVC1-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mvc."/>
 </beans>

控制台

    Jun 12, 2017 12:48:42 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_131\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk1.8.0_131/bin/../jre/bin/client;C:/Program Files/Java/jdk1.8.0_131/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_131/bin/../jre/lib/i386;C:\Program Files\Java\jdk1.8.0_131\bin;;E:\Setup\sts-bundle\sts-3.8.4.RELEASE;;.
Jun 12, 2017 12:48:42 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:MVC1' did not find a matching property.
Jun 12, 2017 12:48:42 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 12, 2017 12:48:42 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jun 12, 2017 12:48:42 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 352 ms
Jun 12, 2017 12:48:42 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 12, 2017 12:48:42 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Jun 12, 2017 12:48:44 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jun 12, 2017 12:48:44 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'MVC1'
Jun 12, 2017 12:48:44 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'MVC1': initialization started
Jun 12, 2017 12:48:44 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'MVC1-servlet': startup date [Mon Jun 12 12:48:44 IST 2017]; root of context hierarchy
Jun 12, 2017 12:48:44 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/MVC1-servlet.xml]
Jun 12, 2017 12:48:45 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'MVC1': initialization completed in 313 ms
Jun 12, 2017 12:48:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jun 12, 2017 12:48:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 12, 2017 12:48:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2616 ms
Jun 12, 2017 2:27:55 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/MVC1/Add] in DispatcherServlet with name 'MVC1'

如何处理这个警告?

【问题讨论】:

  • 你的方法类型在哪里?
  • &lt;form action="Add" method="Post"&gt;
  • 添加一次后检查过吗
  • @RequestMapping(value = {"/Add"}, method = RequestMethod.POST) 添加您的控制器和&lt;form:form action="Add" method="POST" &gt; 在您的表单中
  • &lt;context:component-scan base-package="com.mvc"/&gt; 在 mvc 之后删除 dot

标签: java xml spring spring-mvc


【解决方案1】:

在您的控制器中,您应该在 RequestMapping 中定义 RequestMethod:

@RequestMapping(value = "/Add", method = RequestMethod.GET)

定义好请求方法后,应该通过给出请求方法来调用该方法:

<form:form id="addForm" action="${pageContext.request.contextPath}/Add" method="get">
<button type="submit">Go to Add method</button>
</form:form>

您可以对 Post 请求执行相同的过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-16
    • 2016-01-07
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    相关资源
    最近更新 更多