在Spring MVC中扩展带注释的控制器 [英] Extend annotated controllers in Spring MVC

查看:139
本文介绍了在Spring MVC中扩展带注释的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小项目,并且有一些现有的代码,我希望保持清除我的更改,因此我需要扩展一个带注释的控制器,但这不起作用:

i am working on a small project and have some existing code which I want to keep clean from my changes and therefore I need to extend a annotated controller but this does not work:

package a;

@controller
public class BaseController {
    // Autowired fields

    protected x toExtend() {
         // do stuff
    }

    @RequestMapping(value = "/start")
    protected ModelAndView setupForm(...) {
        toExtend();
        // more stuff
    }
}



package b;

@controller
public class NewController extends BaseController {
    // Autowired fields

    @Override
    protected x toExtend() {
         super.toExtend();
         //new stuff
    }
}

包a和b扫描控制器,我不能改变它。我真的没想到这会起作用,因为@RequestMapping(value =/ start)在两个控制器中都是冗余的。因此我得到了一个例外。

Package a and b are scanned for controllers and i cannont change this. I did not really expect this to work because the @RequestMapping(value = "/start") is redundant in both controllers. And I get an exception because of this.

所以我的问题是,实际上是否可以在不更改BaseController的情况下扩展这样的注释驱动控制器?

So my question is whether it is actually possible to extend a annotation driven controller like this without changing the BaseController?

推荐答案

您可以通过另一个弹簧控制器扩展一个弹簧控制器。

You can extend one spring controller by another spring controller.

当一个春天MVC控制器扩展了另一个Controller,子控制器的功能可以由子控制器使用子控制器的请求URL直接使用。您可以在此处获取更多详细信息扩展Spring控制器

When a Spring MVC controller extends another Controller, the functionality of the base controller can be directly used by the child controller using the request URL of the child controller. You can get more details here Extending Spring controllers

这篇关于在Spring MVC中扩展带注释的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆