Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:@RequestMapping具有多个值_Java_Spring - Fatal编程技术网

Java Spring:@RequestMapping具有多个值

Java Spring:@RequestMapping具有多个值,java,spring,Java,Spring,我不知道这是正确的方法,但这是目前我能想到的最简单的解决方案 我想在@RequestMapping中使用多个值,并根据调用方法的值在方法中执行业务逻辑。例如: @RequestMapping(value = {"/delete", "/save"}, method = RequestMethod.POST) public String crudOps(@ModelAttribute ("userForm") User user) { // find user in repository.

我不知道这是正确的方法,但这是目前我能想到的最简单的解决方案

我想在@RequestMapping中使用多个值,并根据调用方法的值在方法中执行业务逻辑。例如:

@RequestMapping(value = {"/delete", "/save"}, method = RequestMethod.POST)
public String crudOps(@ModelAttribute ("userForm") User user) {

   // find user in repository....

   if(value is delete) // don't know how to make this check
      delete(user);
   else
      save(user);
}

if语句如何工作?

您可以使用@PathVariable获取URL的一部分,如下所示

@RequestMapping(value = /{action}, method = RequestMethod.POST)
 public String crudOps(@PathVariable String action, @ModelAttribute ("userForm") User user) {

       // find user in repository....

       if(action.equals("delete"))
          delete(user);
       else
          save(user);}

您可以使用@PathVariable获取URL的一部分,如下所示

@RequestMapping(value = /{action}, method = RequestMethod.POST)
 public String crudOps(@PathVariable String action, @ModelAttribute ("userForm") User user) {

       // find user in repository....

       if(action.equals("delete"))
          delete(user);
       else
          save(user);}

在添加了上面的注释之后,我想到了一个不同的解决方案,通过访问HttpServletRequest getServletPath方法

@RequestMapping(value = {"/delete", "/save"}, method = RequestMethod.POST)
public String crudOps(@ModelAttribute ("userForm") User user, HttpServletRequest request) {

   // find user in repository....

   if(request.getServletPath().equals("/delete"))
      delete(user);
   else
      save(user);
}

在添加了上面的注释之后,我想到了一个不同的解决方案,通过访问HttpServletRequest getServletPath方法

@RequestMapping(value = {"/delete", "/save"}, method = RequestMethod.POST)
public String crudOps(@ModelAttribute ("userForm") User user, HttpServletRequest request) {

   // find user in repository....

   if(request.getServletPath().equals("/delete"))
      delete(user);
   else
      save(user);
}

我个人会继续做下面的事情

@RequestMapping(value = "user/{userid}", method = RequestMethod.DELETE)
和使用

@RequestMapping(value = "/save", method = RequestMethod.POST)
用于创建用户。这有助于实现单一责任原则和更多模块化代码。让你的方法做一件事,并且做对


将推荐使用哪种HttpMethod用于哪种目的。

我个人会进行如下操作

@RequestMapping(value = "user/{userid}", method = RequestMethod.DELETE)
和使用

@RequestMapping(value = "/save", method = RequestMethod.POST)
用于创建用户。这有助于实现单一责任原则和更多模块化代码。让你的方法做一件事,并且做对


建议使用哪个HttpMethod用于哪个目的。

如果您不提供两种方法,一种用于/delete,另一种用于/save,则需要将另一个参数传递到您的帖子,以指示要执行的操作,例如@RequestParamvalue=delete,required=true boolean delete,除非用户对象中有指示操作的内容,否则我不建议采用这种方法。一种方法应该做一件事。在本例中,在一个方法上映射两个完全不同的操作。这增加了圈复杂度。在这种情况下没有那么多-但我经常在复杂的业务逻辑中看到类似的方法-使其变得更加复杂。如果您不提供两种方法,一种用于/delete,另一种用于/save,则需要将另一个参数传递到您的帖子以指示要执行的操作,例如@RequestParamvalue=delete,required=true boolean delete,除非用户对象中有指示操作的内容,否则我不建议采用这种方法。一种方法应该做一件事。在本例中,在一个方法上映射两个完全不同的操作。这增加了圈复杂度。在这种情况下没有那么多-但我经常在复杂的业务逻辑中看到这样的方法-使其变得更复杂。i动作是删除,当然不编译。i动作是删除,当然不编译。i动作是删除,当然不编译。i动作是删除,当然不编译。抱歉,刚刚更新了答案,我们正在比较stringsI希望@iamiddy的解决方案能起作用,但我发现了一些严重的问题。我确信我的代码有一些问题,但是你的解决方案很有魅力。。。非常感谢。ifaction is delete当然不会编译。很抱歉,刚刚更新了答案,我们正在比较Strings。我希望@iamiddy的解决方案可以正常工作,但我遇到了一些棘手的问题。我确信我的代码有一些问题,但是你的解决方案很有魅力。。。非常感谢。我不知道这是一个老问题,而回答。随机出现在我的问题提要上:我不知道这是一个老问题,而回答。随机出现在我的问题提要上:D