我知道有很多类似的问题。我已附加所有代码,我收到错误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'
如何处理此警告?
答案 0 :(得分:0)
在您的控制器中,您应该在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请求的相同步骤。