gpt4 book ai didi

java - 当子类在Spring MVC中扩展它时,我们可以从父类路径调用@RequestMapping吗?

转载 作者:行者123 更新时间:2023-12-01 16:50:47 45 4
gpt4 key购买 nike

在一次采访中,我被问到一个问题;假设您有一个子 Controller ,它扩展了父 Controller ,如下面的代码所示,并且我们想要访问父类 @RequestMapping 方法。我们可以这样做吗?有什么办法吗?

当我尝试下面的代码并单击 BaseHello 链接时,我收到警告 -如果我在父类中写@Controller,那么也会出现相同的警告

May 09, 2020 2:18:53 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /Projectname/parent/hello

Parent.java

package com.practice;

@RequestMapping("/parent")
public abstract class Parent {
@RequestMapping("/hello")
public String world() {
return "hellopage";
}
}

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

最佳答案

也许这会起作用:


//@RequestMapping("/parent") remove conflicts child path
public abstract class Parent {
@RequestMapping("parent/hello")
public String world() {
return "hellopage";
}}

@Controller
// @RequestMapping("/child") // remove this, add child to the method, because abstract class already have one.
public class Child extends Parent {
@RequestMapping("/child/detail")
public String info() {
return "detailpage";
}}

关于java - 当子类在Spring MVC中扩展它时,我们可以从父类路径调用@RequestMapping吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61694356/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com