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



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

我想破坏控制器功能并使用以下命令对其进行扩展:

StudentFileUploadController  extends FileUpoadController      
LecturerFileUpoadController  extends FileUpoadController        

因此,FileUpoadController 将是抽象的并保持基本功能。

电流控制器:

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

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

@RequestMapping(value = "/uploadStudent") 
@RequestMapping(value = "/uploadLecturer") 

还有其他可能吗?

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

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

最新更新