Java Spring DispatcherServlet:未找到 HTTP 请求的映射

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

Spring DispatcherServlet: No mapping found for HTTP request

javaspringspring-mvc

提问by Randy

I have an issue implementing a very simple page using spring mvc 3.2.4.RELEASE.

我在使用 spring mvc 3.2.4.RELEASE 实现一个非常简单的页面时遇到了问题。

My controller looks like this:

我的控制器看起来像这样:

@Transactional
@Controller
public class MembersDetailsController {
    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/member/{name}", method = RequestMethod.GET)
    public String displayMember(@PathVariable String name) {
        System.out.println(name);
        return "member";
    }

    @RequestMapping(value = "/member", method = RequestMethod.GET)
    public String displayMember() {
        System.out.println("Empty");
        return "member";
    }
}

When I call

当我打电话

http://127.0.0.1:8080/member

the respective method is being executed as desired. However, wenn I call

正在根据需要执行相应的方法。然而,我叫

http://127.0.0.1:8080/member/test

or

或者

http://127.0.0.1:8080/member/test/

I get a 404 with the log-output:

我得到一个带有日志输出的 404:

WARN  org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/member/test] in DispatcherServlet with name 'mvc-dispatcher'

Whats really weired is the a previous log says:

真正奇怪的是之前的日志说:

INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/member/{name}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String 
INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/member],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String

Which means that the mapping should be correct as far as I understand this.

这意味着就我的理解而言,映射应该是正确的。

This is my web.xml:

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!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>Mitgliederdatenbank</display-name>

    <!--Configuration-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-security.xml, /WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>hibernateSessionFactory</param-value>
        </init-param>
    </filter>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <!--Spring Security Filter-->
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <!-- Servlets -->
    <servlet>
        <servlet-name>springGwtRemoteServiceServlet</servlet-name>
        <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
    </servlet>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <!-- Mapping -->
    <servlet-mapping>
        <servlet-name>springGwtRemoteServiceServlet</servlet-name>
        <url-pattern>/ui/springGwtServices/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/welcome</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/logout</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/loginfailed</url-pattern>
    </servlet-mapping>

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

    <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>/login</welcome-file>
  </welcome-file-list>

</web-app>

Could anybody please give me a hint what went wrong here?

有人可以给我一个提示这里出了什么问题吗?

采纳答案by Kre?imir Nesek

I believe that the problem is the same one as described here URL Mapping issue - Spring web MVC.

我相信问题与此处描述的URL 映射问题 - Spring web MVC 相同

Unless you use alwaysUseFullPath spring mvc will match * part to the mapping you specified (e.g. /member/member/test ). See docs for alwaysUseFullPath here (section 17.4) http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html.

除非您使用 alwaysUseFullPath spring mvc 将匹配 * 部分到您指定的映射(例如 /member/member/test )。请参阅此处的 alwaysUseFullPath 文档(第 17.4 节)http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html

However, unfortunately, this property is not exposed through xml configuration element (if you're using xml configuration) so if you'd like your mappings to work the way you discribed in your question you'll need to configure it as described here: http://blog.sarathonline.com/2013/07/enable-alwaysusefullpath-with.html

但是,不幸的是,此属性不会通过 xml 配置元素公开(如果您使用 xml 配置),因此如果您希望映射按照您在问题中描述的方式工作,则需要按照此处所述进行配置:http://blog.sarathonline.com/2013/07/enable-alwaysusefullpath-with.html

回答by macloving

I was faced with the same problem and solved problem by using

我遇到了同样的问题并通过使用解决了问题

<mvc:annotation-driven/>

This tag will configure two beans DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter.

这个标签会配置两个 bean DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter。

Also I added tag to my dispatcher servlet config file.

我还向调度程序 servlet 配置文件中添加了标记。

<context:component-scan base-package="PATH WHERE FIND CONTROLLERS" />