6

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

我的控制器如下所示:

@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";
    }
}

当我打电话

http://127.0.0.1:8080/member

正在根据需要执行相应的方法。但是,我打电话给

http://127.0.0.1:8080/member/test

或者

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

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

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

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

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

这意味着据我了解映射应该是正确的。

这是我的 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>

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

4

2 回答 2

8

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

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

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

于 2013-09-30T17:28:55.647 回答
0

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

<mvc:annotation-driven/>

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

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

<context:component-scan base-package="PATH WHERE FIND CONTROLLERS" />
于 2016-11-26T22:42:20.263 回答