在Spring 4 MVC中是否可以将控制器/具有控制器作为内部类嵌套? [英] Is it possible to nest controllers/have controllers as inner classes in Spring 4 MVC?

查看:272
本文介绍了在Spring 4 MVC中是否可以将控制器/具有控制器作为内部类嵌套?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个映射到/site/的控制器,并且在这两个不同的控制器中看起来像:

I want to have a controller that maps to /site/ and within that two different controllers to look something like:

@Controller
@RequestMapping(value="/api")
public class ApiController {
    @Controller
    @RequestMapping(value="/foo")
    public class FooController {
        //Some /foo/* methods here
    }

    @Controller
    @RequestMapping(value="/bar")
    public class BarController {
       //Some /bar/* methods here
    }

    //Other methods that don't match /foo or /bar
}

这还好吗?还是将它分成具有/site/foo/site/bar映射的两个单独的控制器是更好的做法?

Is this okay or would be it be better practice to split it up into two separate controllers with /site/foo and /site/bar mappings?

推荐答案

将类层次结构与资源层次结构联系起来并不是这里的主要设计驱动力.

Tying your class hierarchy to your resource hierarchy should not be the main design driver here.

在Spring MVC中,控制器是简单的POJO,使其易于测试,组合优于继承,注解用于传达含义并使代码更具可读性.

IN Spring MVC, Controllers are simple POJOs to make them easy to test, composition is favored over inheritance, annotations are used to convey meaning and make your code more readable.

在控制器"下嵌套控制器会破坏这些目标中的几个目标.

Nesting Controllers under Controllers defeats several of those goals.

这篇关于在Spring 4 MVC中是否可以将控制器/具有控制器作为内部类嵌套?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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