Java 警告:在具有名称的 DispatcherServlet 中找不到具有 URI [ ] 的 HTTP 请求的映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21797916/
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 10:42:40  来源:igfitidea点击:

WARNING: No mapping found for HTTP request with URI [ ] in DispatcherServlet with name

javaspringspring-mvc

提问by user93796

I have my web.xml as

我有我的 web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

applicationContext as

应用上下文作为

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

    <context:component-scan base-package="com.mindedges" />


</beans>

Dispatcher servlet as

调度程序 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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

        <!--
        Most controllers will use the ControllerClassNameHandlerMapping above, but
        for the index controller we are using ParameterizableViewController, so we must
        define an explicit mapping for it.
        -->
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="index.htm">indexController</prop>
                </props>
            </property>
        </bean>

        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />

        <!--
        The index controller.
        -->
        <bean name="indexController"
              class="org.springframework.web.servlet.mvc.ParameterizableViewController"
              p:viewName="index" />

    </beans>

Controller as in package com.mindedges.testapp

com. mindges.testapp 包中的控制器

@Controller
@RequestMapping("/sample")
public class SampleController  {

    public String sample(Model model){
        return "sample.jsp";
    }
}

When i hit http://localhost:8084/TestApp/sample.

当我打http://localhost:8084/TestApp/sample.

WARNING: No mapping found for HTTP request with URI [/TestApp/sample] in DispatcherServlet with name

警告:在具有名称的 DispatcherServlet 中找不到具有 URI [/TestApp/sample] 的 HTTP 请求的映射

Also when spring starts I don't see handlers for Testapp/sample loaded

此外,当春天开始时,我没有看到加载了 Testapp/sample 的处理程序

I guess my component scan is not working.Why so? any other reason

我猜我的组件扫描不起作用。为什么会这样?任何其他原因

EDIT: dispatcher-context is

编辑:调度程序上下文是

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

    <context:component-scan base-package="com.mindedges" />

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

采纳答案by Kevin Bowersox

Return the name of the view without the .jspfile extension. The resolution of the view name is handled by the InternalResourceViewResolver.

返回没有.jsp文件扩展名的视图名称。视图名称的解析由InternalResourceViewResolver.

public String sample(Model model){
    return "sample";
}

This snippet of your configuration:

您的配置的这个片段:

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

takes the view name sampleand applies the appropriate prefix (/WEB-INF/jsp/) and suffix (.jsp) to form the entire view name used for resolution of the view.

获取视图名称sample并应用适当的前缀 ( /WEB-INF/jsp/) 和后缀 ( .jsp) 以形成用于视图解析的整个视图名称。

Also make sure the sample.jspis located at /WEB-INF/jsp/sample.jsp.

还要确保sample.jsp位于/WEB-INF/jsp/sample.jsp

Also when using the @RequestMappingannotation you must enable these annotations in the configuration for the dispatcher serlvet by including <mvc:annotation-driven/>.

此外,在使用@RequestMapping注释时,您必须在调度程序 serlvet 的配置中通过包含<mvc:annotation-driven/>.

You must also add component scanning within the dispatcher configuration so that the controllers are registered with the dispatcher servlet. Currently, you perform component scanning within the applicationContext.xmlconfiguration file using:

您还必须在调度程序配置中添加组件扫描,以便将控制器注册到调度程序 servlet。目前,您在applicationContext.xml配置文件中执行组件扫描使用:

<context:component-scan base-package="com.mindedges" />

However, since this component scanning occurs within the applicationContextfile the registered beans (assuming they are your controllers) will not be available to the dispatcher serlvet. You must place this configuration snippet within dispatcher-context.xml.

但是,由于此组件扫描发生在applicationContext文件中,因此已注册的 bean(假设它们是您的控制器)将无法用于调度程序 serlvet。您必须将此配置片段放在dispatcher-context.xml.

You many want to consult one of my previous answersregarding the differences between the dispatcher configuration and the context configuration.

关于调度程序配置和上下文配置之间的差异,许多人想参考我之前的答案之一。

There was also some whitespace in your dispatcher-context.xml file and a the mvcnamespace was missing. I have corrected these issues and provided them in this GitHub Gist.

您的 dispatcher-context.xml 文件中还有一些空格,并且mvc缺少命名空间。我已经更正了这些问题并在这个GitHub Gist 中提供了它们。

回答by akhilesh joshi

Change the name of the request map, have a different name from the jsp it is trying to return

更改请求映射的名称,与它尝试返回的 jsp 使用不同的名称