404错误org.springframework.web.servlet.PageNotFound noHandlerFound

时间:2015-12-13 18:45:05

标签: spring spring-mvc

在我的spring configuratin文件中,我已将此代码<mvc:view-controller path="/results" view-name="results" />编写为map&#39; / results&#39; to viewName&#39; results&#39;。它的工作正常。但是控制器中的以下映射不起作用。

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

在访问此网址http://localhost:8090/SpringFormValidation/form时抛出404错误。

当我删除此代码<mvc:view-controller path="/results" view-name="results" />时,控制器中的映射工作正常。

我只是一无所知,是什么造成的?

我发布了我的控制台输出,Controller类和spring配置文件。

我的控制器类:

package com.github.binoybaranwal.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.github.binoybaranwal.pojo.Person;

@Controller
public class WebController extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/results").setViewName("results");
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String showForm(Person person){      
        return "form";
    }

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String checkPersonInfo(@Valid Person person, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return "form";
        }
        return "redirect:/results";
    }

}

我的弹簧配置文件 SpringFormValidation-servlet.xml中

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

    <context:component-scan base-package="com.github.binoybaranwal.controller" />

    <tx:annotation-driven/>     

    <mvc:view-controller path="/results" view-name="results" />


</beans>

我的控制台输出:

Dec 14, 2015 12:18:28 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringFormValidation' did not find a matching property.
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.59
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jan 28 2015 15:51:10 UTC
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.59.0
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.2
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre7
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.7.0_79-b15
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\wtpwebapps
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\endorsed
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dorg.apache.el.parser.SKIP_IDENTIFIER_CHECK=true
Dec 14, 2015 12:18:28 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Dec 14, 2015 12:18:28 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
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\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Program Files\Broadcom\Broadcom 802.11 Network Adapter;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Sony\VAIO Improvement;C:\Program Files (x86)\Sony\VAIO BIOS Data Transfer Utility\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\Skype\Phone\;C:\Program Files (x86)\Git\cmd;C:\apache-maven-3.3.9\bin;.
Dec 14, 2015 12:18:29 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8090"]
Dec 14, 2015 12:18:29 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Dec 14, 2015 12:18:29 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 982 ms
Dec 14, 2015 12:18:29 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Dec 14, 2015 12:18:29 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.59
Dec 14, 2015 12:18:31 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Dec 14, 2015 12:18:31 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Dec 14, 2015 12:18:31 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Dec 14, 2015 12:18:31 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Mon Dec 14 00:18:31 IST 2015]; root of context hierarchy
Dec 14, 2015 12:18:31 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/restws-service-context.xml]
Dec 14, 2015 12:18:31 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 509 ms
Dec 14, 2015 12:18:32 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [307] milliseconds.
Dec 14, 2015 12:18:32 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'SpringFormValidation'
Dec 14, 2015 12:18:32 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'SpringFormValidation': initialization started
Dec 14, 2015 12:18:32 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'SpringFormValidation-servlet': startup date [Mon Dec 14 00:18:32 IST 2015]; parent: Root WebApplicationContext
Dec 14, 2015 12:18:32 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/SpringFormValidation-servlet.xml]
Dec 14, 2015 12:18:32 AM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
INFO: Mapped URL path [/results] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
Dec 14, 2015 12:18:33 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'SpringFormValidation': initialization completed in 854 ms
Dec 14, 2015 12:18:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\binoy
Dec 14, 2015 12:18:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\binoy has finished in 92 ms
Dec 14, 2015 12:18:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\docs
Dec 14, 2015 12:18:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\docs has finished in 78 ms
Dec 14, 2015 12:18:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\examples
Dec 14, 2015 12:18:33 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Dec 14, 2015 12:18:33 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Dec 14, 2015 12:18:33 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@6c0f5c75')
Dec 14, 2015 12:18:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\examples has finished in 591 ms
Dec 14, 2015 12:18:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\host-manager
Dec 14, 2015 12:18:34 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\host-manager has finished in 96 ms
Dec 14, 2015 12:18:34 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\manager
Dec 14, 2015 12:18:34 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\manager has finished in 219 ms
Dec 14, 2015 12:18:34 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\ROOT
Dec 14, 2015 12:18:34 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory C:\apache-tomcat-7.0.59-windows-x86\apache-tomcat-7.0.59\webapps\ROOT has finished in 70 ms
Dec 14, 2015 12:18:34 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8090"]
Dec 14, 2015 12:18:34 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Dec 14, 2015 12:18:34 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5288 ms
Dec 14, 2015 12:18:36 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringFormValidation/form] in DispatcherServlet with name 'SpringFormValidation'
Dec 14, 2015 12:18:38 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringFormValidation/form] in DispatcherServlet with name 'SpringFormValidation'
Dec 14, 2015 12:18:54 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/SpringFormValidation] has started
Dec 14, 2015 12:18:54 AM org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'SpringFormValidation'
Dec 14, 2015 12:18:54 AM org.springframework.web.context.support.XmlWebApplicationContext doClose
INFO: Closing WebApplicationContext for namespace 'SpringFormValidation-servlet': startup date [Mon Dec 14 00:18:32 IST 2015]; parent: Root WebApplicationContext
Dec 14, 2015 12:18:54 AM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Dec 14, 2015 12:18:54 AM org.springframework.web.context.support.XmlWebApplicationContext doClose
INFO: Closing Root WebApplicationContext: startup date [Mon Dec 14 00:18:31 IST 2015]; root of context hierarchy
Dec 14, 2015 12:18:55 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Dec 14, 2015 12:18:55 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Dec 14, 2015 12:18:55 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Dec 14, 2015 12:18:55 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Mon Dec 14 00:18:55 IST 2015]; root of context hierarchy
Dec 14, 2015 12:18:55 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/restws-service-context.xml]
Dec 14, 2015 12:18:56 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 478 ms
Dec 14, 2015 12:18:56 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'SpringFormValidation'
Dec 14, 2015 12:18:56 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'SpringFormValidation': initialization started
Dec 14, 2015 12:18:56 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'SpringFormValidation-servlet': startup date [Mon Dec 14 00:18:56 IST 2015]; parent: Root WebApplicationContext
Dec 14, 2015 12:18:56 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/SpringFormValidation-servlet.xml]
Dec 14, 2015 12:18:56 AM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
INFO: Mapped URL path [/results] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
Dec 14, 2015 12:18:56 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'SpringFormValidation': initialization completed in 638 ms
Dec 14, 2015 12:18:56 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/SpringFormValidation] is completed
Dec 14, 2015 1:16:21 AM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringFormValidation/form] in DispatcherServlet with name 'SpringFormValidation'

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

通过在我的控制器中添加注释@EnableWebMvc来修复它。感谢