org.springframework.web.servlet.DispatcherServlet noHandlerFound 404错误响应

时间:2019-06-08 20:19:58

标签: java spring maven web model-view-controller

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>sample</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sample</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我在其中添加了表单和操作“输出”的索引文件

<html>
<body>  <form action="output">
        <input type="text" name="t1"></br> <input type="text" name="t2"></br>
        <input type="submit">
    </form>
</body>
</html>

该servlet的配置如下

<?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:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.guni.Controllers"></context:component-scan>
</beans>

sampleController.java

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

@Controller
public class sampleController { 
    @RequestMapping(value="/output", method = RequestMethod.GET)
    public String karna()
    {
        return "sampleOutput.jsp";
    }
}

我正在尝试使用提交按钮执行表单GET。提交按钮没有任何反应。该网页给我404错误。任何人都有想法,我正在使用eclipse 2019和tomcat9。我正在尝试使用Spring 4.0在Maven中创建Web应用程序

运行项目时,出现以下错误     org.springframework.web.servlet.DispatcherServlet noHandlerFound    GET / demoMVC / output没有映射

1 个答案:

答案 0 :(得分:0)

您的控制器方法已注册,可以在/output上执行GET请求时接受/demoMVC/output上的GET请求。将您的@RequestMapping值修改为/demoMVC/output,或更改您在浏览器中调用的URL。

此外,在许多情况下,InternalResourceViewResolver被配置为返回不带后缀.jsp的JSP文件的名称。假设您已经创建了一个名为sampleOutput.jsp的文件,请调整您的karna()方法以返回"sampleOutput"而不是"sampleOutput.jsp"。如果这样做不起作用,您可以发布您的InternalResourceViewResolver配置吗?