【发布时间】:2020-05-09 09:14:59
【问题描述】:
在一次采访中,我被问到一个问题;假设您有一个子控制器,它像下面的代码一样扩展父控制器,并且我们想要访问父类 @RequestMapping 方法。我们可以这样做吗?有什么办法吗?
当我尝试下面的代码并单击 BaseHello 链接时,我收到警告- 如果我在 Parent 类中写 @Controller 那么同样的警告也会出现
May 09, 2020 2:18:53 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /Projectname/parent/hello
父类.java
package com.practice;
@RequestMapping("/parent")
public abstract class Parent {
@RequestMapping("/hello")
public String world() {
return "hellopage";
}
}
子.java
package com.practice;
@Controller
@RequestMapping("/child")
public class Child extends Parent {
@RequestMapping("/detail")
public String info() {
return "detailpage";
}
}
index.jsp
<a href="parent/hello">BaseHello</a>
<a href="child/hello">ChildHello </a>
<a href="child/details">ChildDetails</a>
【问题讨论】:
标签: java spring-mvc controller