MVC - 将控制器分解为子类层次结构

时间:2017-07-24 02:17:49

标签: spring spring-mvc

我目前有一个单一的控制器(使用Spring mvc实现),供学生和老师上传文件控制器名称为FileUpoadController

我想打破控制器功能并使用以下方法扩展它:

"1,5|6,45|51,3|54,45|99,45|144,45|189,60|249,15|264,11|275,60| _

因此FileUpoadController将是抽象的并维护基本功能。

电流控制器:

 StudentFileUploadController  extends FileUpoadController      
 LecturerFileUpoadController  extends FileUpoadController        

一种解决方案是使用两个控制器进行不同的上传映射

@Controller
@RequestMapping(value = "/upload") 
public class FileUpoadController 

有可能以其他方式吗?

1 个答案:

答案 0 :(得分:1)

除了不同的映射之外,这些方法的行为非常不可能完全相同。但您可以改为使用委托:

public class FileUpoadController<T> {
    public List<T> getList(){
        // returns list of T
    }
}

@Controller(value = "/uploadStudent")
public class UploadStudentController extends FileUpoadController<UploadStudent>{
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public @ResponseBody List<UploadStudent> getStudent() {
      return super.getList();
    }    
}

@Controller(value = "/uploadLecturer")
public class UploadLecturerController extends FileUpoadController<UploadLecture>{
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public @ResponseBody List<UploadLecture> getLecture() {
      return super.getList();
    }
}

有关详细信息,请参阅: https://www.codeproject.com/Articles/799677/The-Hierarchy-of-Controller-Class-in-ASP-NET-MVC