Java spring-mvc - 找不到带有 URI 的 HTTP 请求的映射

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

spring-mvc - No mapping found for HTTP request with URI

javaspringspring-mvc

提问by bglow

I know that there were dozens of similar topics, but I couldn't find solution in any of it. I want to make simple 'hello world' app using spring mvc. It gives me 404 with description: The requested resource is not available. In the console:

我知道有几十个类似的主题,但我无法在其中任何一个中找到解决方案。我想使用 spring mvc 制作简单的“hello world”应用程序。它给了我 404 的描述:请求的资源不可用。在控制台中:

wrz 13, 2013 7:38:14 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/gitsystem/] in DispatcherServlet with name 'dispatcher'

Controller.java:

控制器.java:

package bg.glowacki.gitsystem.controller;

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

@Controller
public class Controller {

    @RequestMapping("/")
    public String printHelloWorld(Model model) {
        model.addAttribute("message", "Hello World!");

        return "helloWorld";
    }
}

dispatcher-servlet.xml

调度程序-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:p="http://www.springframework.org/schema/p"
    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.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="bg.glowacki.gitsystem.controller" />
    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

Request

要求

GET /gitsystem/ HTTP/1.1 
Host: localhost:8080 
Connection: keep-alive 
Cache-Control: max-age=0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

回答by CodeChimp

By default, most app servers will make the URL to your servlet something like <context>/<servlet_path>/<rest_of_mapping>, where <context>is usually the WAR name. In your example code, you don't have any mapping on the Controller at all, and "/" on the method, but you somehow expect /gitsystemto make it to your handler? It doesn't work that way. Not to mention, your sample request doesn't have any Context portion, unless they WAR you are deploying is called gitsystem.war.

默认情况下,大多数应用服务器会将您的 servlet 的 URL 设为类似于<context>/<servlet_path>/<rest_of_mapping>,其中<context>通常是 WAR 名称。在您的示例代码中,您在 Controller 上根本没有任何映射,并且在该方法上没有任何映射,但是您以某种方式希望/gitsystem将其传递给您的处理程序?它不会那样工作。更不用说,您的示例请求没有任何上下文部分,除非您正在部署的 WAR 称为 gitsystem.war。

First, you should have SOMETHING on your Controller. Second, you should have something on your handler as well. Although you can modify the Context portion to not be the WAR name, you cannot get rid of the Context altogether, at least not that I know of. After the app server matches the Context, it will match the servlet. In your case, you mapped that to '/', which is fine. After that, the DispatchServlet takes care of the rest. At this point, the DispatchServlet is just matching the request to a Controller using its @RequestMapping, then to the handler using it's @RequestMapping. In your case, you ommited the first, and made the second nothing. I just don't think that will work.

首先,您的控制器上应该有一些东西。其次,你的处理程序也应该有一些东西。尽管您可以将 Context 部分修改为不是 WAR 名称,但您不能完全摆脱 Context,至少我不知道。在应用服务器匹配 Context 后,它将匹配 servlet。在您的情况下,您将其映射到“/”,这很好。之后, DispatchServlet 负责其余的工作。此时, DispatchServlet 只是使用它的 将请求匹配到控制器@RequestMapping,然后使用它的匹配处理程序@RequestMapping。在您的情况下,您省略了第一个,而第二个什么也没做。我只是不认为那会奏效。

回答by Sotirios Delimanolis

Unless you haven't shown us the true code, this is the only explanation I can think of.

除非你没有向我们展示真正的代码,这是我能想到的唯一解释。

import org.springframework.stereotype.Controller;

@Controller
public class Controller {

    @RequestMapping("/")
    public String printHelloWorld(Model model) {
        model.addAttribute("message", "Hello World!");

        return "helloWorld";
    }
}

In the above code, org.springframework.stereotype.Controlleris conflicting with the name of your class which is also Controller.

在上面的代码中,org.springframework.stereotype.Controller与您的班级名称冲突,该名称也是Controller.

I don't know how you are compiling and building this application, but the class file won't end up in the classpath and therefore your component-scanwon't find it and register it as a handler. Therefore you have nothing handling the path /. And you get

我不知道你是如何编译和构建这个应用程序的,但是类文件不会在类路径中结束,因此你component-scan不会找到它并将它注册为处理程序。因此,您无需处理 path /。你得到

WARNING: No mapping found for HTTP request with URI [/gitsystem/] in DispatcherServlet with name 'dispatcher'

If you enable logging within your application, you will notice there is no bean created of type bg.glowacki.gitsystem.controller.Controller.

如果您在应用程序中启用日志记录,您会注意到没有创建类型为 的 bean bg.glowacki.gitsystem.controller.Controller

Rename your class to something else that doesn't have a naming conflict with any of the imports.

将您的类重命名为与任何导入都没有命名冲突的其他名称。

回答by zpontikas

Change your servlet-mapping in web.xml and add a * in it

更改 web.xml 中的 servlet-mapping 并在其中添加 *

 <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
 </servlet-mapping>

回答by Ashok

change the your servlet name dispatcher to any other name .because dispatcher is predefined name for spring3,spring4 versions.

将您的 servlet 名称调度程序更改为任何其他名称。因为调度程序是 spring3、spring4 版本的预定义名称。

example

例子

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