警告:在名称为“ dispatcherservlet”的DispatcherServlet中找不到带有URI [/ mvc / add]的HTTP请求的映射

时间:2018-09-13 14:05:11

标签: java xml spring spring-mvc spring-annotations

我看到了类似的问题,并尝试将URL从/ *映射到/,但此方法不起作用。 我还尝试直接从浏览器中访问URL,但它也不起作用。 我是Spring的新手,有人可以在这里帮助我。

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcherservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
    </init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherservlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

enter image description here

而我的dispatcherservlet-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: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/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:resources mapping="/style/**" location="/style/"/>
<mvc:default-servlet-handler/>

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

最后是我的控制器

    package com;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class addition {

    @RequestMapping("/add")
 void add() {
     System.out.println("im addint");
 }
}

我的index.jsp作为欢迎文件

 <html>
<body>
<h2>Hello World!</h2>
<form action="add">
<input type="text" name="first number">first number<br>
<input type="text" name="second number">second number<br>
<input type="submit" name="add two numbers"><br>

</form>
</body>
</html>

当我运行时,我可以提交并点击添加方法的URL,但是却得到警告:在DispatcherServlet中,名称为“ dispatcherservlet”的URI [/ mvc / add]找不到HTTP请求的映射 < / p>

1 个答案:

答案 0 :(得分:1)

您在<mvc:annotation-driven />上缺少dispatcherservlet-servlet.xml,因此启用了@Controller注释。