我在Spring PetClinic 示例应用程序的 OwnerController 类中添加了以下方法:
//'''''''''CodeMed added this next method
@RequestMapping(value = "/owners/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(Map<String, Object> model) {
// find owners of a specific type of pet
Integer typeID = 1;//this is just a placeholder
Collection<Owner> results = this.clinicService.findOwnerByPetType(typeID);
model.put("selections", results);
return "owners/catowners";
}
//'''''''''''''''''''
由于 petclinic 数据库中猫的 typeID 为 1,因此上面返回了猫主人的列表。但我也想在网站上为狗主人、蜥蜴主人、仓鼠主人和任何其他宠物的主人创建单独的页面。我需要为每种宠物类型创建一个单独版本的 findOwnersOfPetType() 吗?像 findDogOwners()、findLizardOwners()、findHamsterOwners() 等?或者我可以让 findOwnersOfPetType() 方法接受一个指示宠物类型的 int 参数吗?
jsp文件呢?我需要为 catowners.jsp、dogowners.jsp、lizardowners.jsp、hamsterowners.jsp 等每个文件创建一个单独的 jsp 文件吗?或者我可以以某种方式创建一个jsp,为每种类型的宠物填充相同格式的不同数据?
这在代码中看起来如何?
ClinicService 和 OwnerRepository 函数已经一起处理了,因为我上面发布的函数使用函数中创建的参数调用了 ClinicService 方法。