【发布时间】:2015-07-22 18:35:50
【问题描述】:
我有一个ImportAction 类,它作为几个特定类型导入控制器的父类,例如ImportClientsAction 和ImportServicesAction。
ImportAction 是一个带有 @Controller 注释的 Spring MVC 类,并具有 @RequestMapping-annotated 方法来拉出导入选项菜单并输入每个特定于类型的导入控制器。
每个子类,例如ImportClientsAction 也被注解为 @Controller 并且具有特定类型的 @RequestMappings 用于其类型的特定导入过程。
任何子类中的@RequestMappings 都不应与父类或彼此发生冲突;每个都有不同的路径/值和不同的参数。
根据我在this one 和this one 等问题中遇到的情况,听起来Spring 将每个子类都视为具有父类的@RequestMapping 注释方法的副本,即使子类确实如此不覆盖父级的方法。
有没有办法让@Controller-annotated 父类与@RequestMappings,并拥有@Controller-annotated 子类,而 Spring 不会将子类视为重复父类的 @RequestMapping-annotated 方法?
额外的问题,为什么 Spring 不能识别子类上的 @RequestMapping“重复”,而忽略除父版本之外的所有版本?这是否根本没有实现,或者 Java 中是否存在使这不可能实现的基本问题?
编辑:示例代码
父类示例:
@Controller
public class ImportAction {
@RequestMapping(value = "/import", params = "m=importMenu", method = RequestMethod.GET)
public String importMenu(HttpServletRequest request) throws Exception {
return TilesConstants.IMPORT_MENU;
}
@RequestMapping(value = "/import", params = "m=importClients", method = RequestMethod.GET)
public String importClients(@ModelAttribute("ImportUploadForm") ImportUploadForm theForm, HttpServletRequest request) throws Exception {
retrieveReturnPage(request);
theForm.setSomeBoolean(true);
return TilesConstants.IMPORT_CLIENTS_UPLOAD;
}
@RequestMapping(value = "/import", params = "m=importServices", method = RequestMethod.GET)
public String importServices(@ModelAttribute("ImportUploadForm") ImportUploadForm theForm, HttpServletRequest request) throws Exception {
retrieveReturnPage(request);
theForm.setSomeBoolean(false);
return TilesConstants.IMPORT_SERVICES_UPLOAD;
}
/* etc 7 more almost identical methods */
}
子类示例:
@Controller
public class ImportClientsAction extends ImportAction {
@RequestMapping(value = "/importClients", params = "m=uploadClients", method = RequestMethod.POST)
public String uploadClients(@ModelAttribute("ImportUploadForm") ImportUploadForm theForm, BindingResult errors, HttpServletRequest request) throws Exception {
if (!parseAndStoreUploadedFile(theForm, errors, request)) {
return TilesConstants.IMPORT_CLIENTS_UPLOAD;
}
return "redirect:/importClients?m=enterMapClientsUpload";
}
/* etc other "client" type-specific import methods */
}
【问题讨论】:
-
您能发布一个完整且可重现的示例吗?
-
如果你覆盖了父母的方法;否则,它是模棱两可的。
-
@SotiriosDelimanolis 完成。
-
是的,您将不得不以某种方式拆分处理程序方法。因为超类是具体的(而不是抽象的)并且使用
@Controller注释,所以Spring 将为它创建一个实例并注册它的方法。它会为继承了这些方法的子类做同样的事情。 -
当您有两个子类并且每个子类都提供一个
@Controllerbean 时,问题变得更加明显。它们都从超类继承了一个处理程序方法。 Spring MVC 使用哪个实例作为方法调用的目标?
标签: java spring-mvc inheritance controller