Java 警告:org.springframework.web.servlet.PageNotFound - 在 DispatcherServlet 中找不到名称为“spring”的带有 URI [] 的 HTTP 请求的映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21218071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:23:58  来源:igfitidea点击:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [] in DispatcherServlet with name 'spring'

javaspringmavenspring-mvccontroller

提问by svirfneblyn

Please, help to find my mistakes. I've got error "WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [] in DispatcherServlet with name 'spring'"

请帮助找出我的错误。我有错误“警告:org.springframework.web.servlet.PageNotFound - 在 DispatcherServlet 中找不到名称为 'spring' 的 URI [] 的 HTTP 请求的映射”

web.xml

网页.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Test MVC WEB</display-name>

  <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.xml</param-value>
    </context-param>
    <listener>
        <listener-class>o`enter code  here`rg.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

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

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
<!--     <welcome-file>index.htm</welcome-file> -->
<!--     <welcome-file>index.jsp</welcome-file> -->
<!--     <welcome-file>default.html</welcome-file> -->
<!--     <welcome-file>default.htm</welcome-file> -->
<!--     <welcome-file>default.jsp</welcome-file> -->
  </welcome-file-list>
</web-app>

spring-servlet.xml

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




    <context:annotation-config />
    <mvc:annotation-driven />
    <tx:annotation-driven /> 
<!--    <context:component-scan base-package="by.rubanovich" />  -->


    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/config.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean> 
</beans>

and controller

和控制器

package by.rubanovich.controller;

import java.util.Map;

//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import by.rubanovich.model.Person;
import by.rubanovich.service.PersonService;

@Controller
public class DataController {
//@Autowired
    private PersonService personService;

    @RequestMapping("/index")
    public String setupForm(Map<String, Object> map) {
        Person person = new Person();
        map.put("person", person);
        map.put("personList", personService.getAllPersons());
        return "person";
    }

    @RequestMapping(value = "/person.do", method = RequestMethod.POST)
    public String doAction(@ModelAttribute Person person, BindingResult result,
            @RequestParam String action, Map<String, Object> map) {
        Person personResult = new Person();

        switch (action.toLowerCase()) {

        case "add":
            personService.add(person);
            personResult = person;
            break;
        case "edit":
            personService.edit(person);
            personResult = person;
            break;
        case "delete":
            personService.delete(person.getPersonId());
            personResult = new Person();
        case "search":
            Person searchPerson = personService.getPerson(person.getPersonId());
            personResult = searchPerson != null ? searchPerson : new Person();
            break;
        default:
            break;
        }
        map.put("person", personResult);
        map.put("personList", personService.getAllPersons());
        return "person";
    }
}

采纳答案by Jean-Philippe Bond

It seem that the package that contains controllers is not scan by the application.

应用程序似乎没有扫描包含控制器的包。

Uncomment that line in spring-servlet.xml:

取消注释该行spring-servlet.xml

<context:component-scan base-package="by.rubanovich" />

EDIT

编辑

Add this to you web.xml

添加这个给你 web.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

And correct this listener :

并纠正这个听众:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

回答by Yacine

I think that you've a problem on your url. Because your controller is configured to manage requests by @RequestMapping("/index")

我认为您的网址有问题。因为您的控制器配置为通过以下方式管理请求@RequestMapping("/index")

So if your want to access your project /TestJavaMvcWeb/you have to:

因此,如果您想访问您的项目,/TestJavaMvcWeb/您必须:

  1. change your RequestMapping("/index")to RequestMapping("/")or
  2. access your urlby /TestJavaMvcWeb/index
  1. 将您更改RequestMapping("/index")RequestMapping("/")
  2. 访问你url/TestJavaMvcWeb/index

回答by Rajesh2389

in my case i have more than one Controller and I got this same problem. After removing a class level request mapping for the controller from where the issue arised solved the problem for me.

就我而言,我有不止一个控制器,我遇到了同样的问题。在从出现问题的地方删除控制器的类级别请求映射后,我解决了这个问题。