spring 未找到带有 URI 的 HTTP 请求的映射.... 在 DispatcherServlet 中,名称为

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

No mapping found for HTTP request with URI.... in DispatcherServlet with name

springspring-mvc

提问by erik.c

I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.

我已经查看了几乎所有关于 stackoverflow 的相关文章,但我无法解决我的问题。

Here is the code: web.xml:

这是代码:web.xml:

   <display-name>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>/</url-pattern>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.xml:

弹簧servlet.xml:

<context:component-scan base-package="com.mycompany.elso" />
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>   

    <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>

myController:

我的控制器:

public class myController {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message); 
    }
}

Web Pages/index.jsp:

网页/index.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series</title>
</head>
<body>
    <a href="hello.html">Say Hello</a>
</body>
</html>

Web Pages/WEB-INF/jsp/hello.jsp:

网页/WEB-INF/jsp/hello.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
    ${message}
</body>
</html>

So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:

因此,当我启动应用程序时,index.jsp 已正确加载,但是当我单击 href 导航到 hello.jsp 时,出现 404 错误并且服务器日志显示:

No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'

I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?

我已经检查了几十篇这样的文章,但我找不到错误,有人知道它是什么吗?

采纳答案by Nils

You could try and add an @Controllerannotation on top of your myController Class and try the following url /<webappname>/my/hello.html. This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMappingprepends /myto each RequestMapping in the myController class.

您可以尝试@Controller在 myController 类的顶部添加注释并尝试以下 url /<webappname>/my/hello.html。这是因为在 myController 类中的每个 RequestMappingorg.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping前面都添加了/my

回答by Harry

Add

添加

  <mvc:default-servlet-handler/>

to spring-servlet.xml

到 spring-servlet.xml

回答by ssk

If you are using

如果您正在使用

<mvc:annotation-driven/> 

make sure your spring-servlet.xml has correct

确保你的 spring-servlet.xml 有正确的

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

Basically, you need to include all the packages where you have used the annotation in your java code.

基本上,您需要在 Java 代码中包含使用注释的所有包。

Also, please ensure you do not have duplication of component-scan(for a discovery of beans). If your config XML already contains the element, then any of your Controller classes that are annotated with @ComponentScan(basePackages=... needs to be stripped of the said annotation.

另外,请确保您没有重复的组件扫描(用于发现 bean)。如果您的配置 XML 已经包含该元素,那么任何使用@ComponentScan(basePackages=...进行注释的 Controller 类都需要从所述注释中删除。

回答by ASHISH GUPTA

I solved my issue with : Java Build Path -> JRE system library - > Edit -> Alternate JRE -> -> Finish

我解决了我的问题:Java Build Path -> JRE system library -> Edit -> Alternate JRE -> -> Finish

As it was configured to JDK folder so it was giving Exception

因为它被配置为 JDK 文件夹,所以它给出了 Exception

回答by prayagupd

Make sure

确保

<mvc:annotation-driven/>
<context:component-scan base-package="com.hireartists.web.controllers"/>

points to proper package that contains controllers.

指向包含控制器的正确包。

回答by kishore

Please check your [PROJECT_NAME]\target\classesdirectory to see whether myController.classis generated or not.

请检查您的[PROJECT_NAME]\target\classes目录以查看是否myController.class已生成。

If not, please check all your java source code whether there are any compilation errors.

如果没有,请检查您所有的java源代码是否有任何编译错误。

回答by Udeep Shakya

Try:

尝试:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Worked for me!  

对我来说有效!  

回答by Gaurav Goyal

If you are using Java code based on Spring MVC configuration then enable the DefaultServletHandlerConfigurerin the WebMvcConfigurerAdapterobject.

如果您使用的是基于 Spring MVC 配置的 Java 代码,则DefaultServletHandlerConfigurerWebMvcConfigurerAdapter对象中启用。

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
        configurer.enable();
}

回答by Vishal Gupta

Check ur Bean xmlns..

检查你的 Bean xmlns ..

I also had similar problem, but I resolved it by adding mvc xmlns.

我也有类似的问题,但我通过添加 mvc xmlns 解决了它。

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p"
    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.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="net.viralpatel.spring3.controller" />
    <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>
</beans>

回答by Nilay Deshmukh

addition of <mvc:annotation-driven/>worked for me. Add it before line <context:component-scan ............/>

另外<mvc:annotation-driven/>对我有用。在行前添加<context:component-scan ............/>