简体   繁体   English

Spring控制器方法使用不同的参数共享我的多个@RequestMapping url

[英]Spring controller method shared my multiple @RequestMapping urls with different parameters

I have added the following method to the OwnerController class in the Spring PetClinic sample application : 我已将以下方法添加到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";
 }
//'''''''''''''''''''

Since the typeID for cats in the petclinic database is 1, the above returns a list of cat owners. 由于petclinic数据库中cat的typeID为1,因此上面返回了cat所有者列表。 But I also want to create separate pages on the site for dog owners, lizard owners, hamster owners, and owners of any other kind of pet. 但我也想在网站上为狗主人,蜥蜴主人,仓鼠主人和任何其他宠物的主人创建单独的页面。 Do I need to create a separate version of findOwnersOfPetType() for each pet type? 我是否需要为每种宠物类型创建一个单独版本的findOwnersOfPetType()? Like findDogOwners(), findLizardOwners(), findHamsterOwners(), etc.? 像findDogOwners(),findLizardOwners(),findHamsterOwners()等? Or can I have the findOwnersOfPetType() method accept an int parameter indicating the pet type? 或者我可以让findOwnersOfPetType()方法接受一个指示宠物类型的int参数吗?

What about jsp files? 那些jsp文件呢? Do I need to create a separate jsp file for each of catowners.jsp, dogowners.jsp, lizardowners.jsp, hamsterowners.jsp, etc.? 我是否需要为catowners.jsp,dogowners.jsp,lizardowners.jsp,hamsterowners.jsp等中的每一个创建单独的jsp文件? Or can I somehow create one jsp that is populated with different data in the same format for each type of pet? 或者我可以以某种方式为每种类型的宠物创建一个以相同格式填充不同数据的jsp?

How would this look in code? 这看起来如何代码?

The ClinicService and OwnerRepository functions are already handled together because the function I posted above calls a ClinicService method using a parameter created in the function. ClinicService和OwnerRepository函数已经一起处理,因为我上面发布的函数使用函数中创建的参数调用ClinicService方法。

You can use a RequestMapping with a PathVariable . 您可以将RequestMappingPathVariable一起使用。 For example: 例如:

@RequestMapping(value = "/owners/{petId}", method = RequestMethod.GET)
public String findOwnersOfPetType(Map<String, Object> model,
    @PathVariable int petId) {
    //use id as before
}

If you want to use strings in your URLs instead of integers, you can make them more user friendly by using Strings in the URLs instead and having an enum mapping between those Strings and their IDs. 如果要在URL中使用字符串而不是整数,可以通过在URL中使用字符串并在这些字符串与其ID之间进行枚举映射,使它们更加用户友好。

You can add a type parameter to your request mapping: 您可以在请求映射中添加类型参数:

@RequestMapping(value = "/owners/{type}", method = RequestMethod.GET)
public String findOwnersOfPetType(@PathVariable("type") it type) {

}

So you don't need different controller methods for handling multiple types. 因此,您不需要不同的控制器方法来处理多种类型。

The service method depends on how you model your domain objects. 服务方法取决于您对域对象的建模方式。 If you have a single class Pet which contains a petType you could easily do something like this: 如果您有一个包含petType Pet类,您可以轻松地执行以下操作:

Collection<Owner> results = this.clinicService.findOwnerByPetType(type);

The service then calls the repository method findOwnerByPetType(type) which returns the list of owners 然后,该服务调用存储库方法findOwnerByPetType(type) ,该方法返回所有者列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM