简体   繁体   English

Spring MVC多个请求映射,缺少请求参数

[英]Spring MVC multiple requestmapping , missing requestparam

I need to handle two @RequestMapping values by one method.For example /create and create/{id} 我需要用一种方法处理两个@RequestMapping值。例如/ create和create / {id}

@RequestMapping(value = {"create","create/{id}"}, method = RequestMethod.GET)
public String create_form(@PathVariable(value = "id") Long id,Model model, @ModelAttribute("channelNode") ChannelNode channelNode,
        BindingResult result) {

      if(id>0){ //or if id exsist 

      //do something

      }

    return CHANNELNODE_ADD_VIEW;
}

But it doesn work when I run simple "create" url, without any param/ 但是当我运行简单的“创建”网址而没有任何参数/时,此方法不起作用

It shows me following Error: 它显示以下错误:

HTTP Status 500 - Missing URI template variable 'id' for method parameter of type Long HTTP状态500-类型为Long的方法参数缺少URI模板变量'id'

type Status report 类型状态报告

message Missing URI template variable 'id' for method parameter of type Long 消息缺少类型为Long的方法参数的URI模板变量'id'

description The server encountered an internal error that prevented it from fulfilling this request. 描述服务器遇到内部错误,导致服务器无法满足此请求。

Unfortunately there is not way you could do this with @PathVariable . 不幸的是,您无法使用@PathVariable做到这@PathVariable

You need to do it by defining 2 separate handler methods, 您需要通过定义2个单独的处理程序方法来做到这一点,

  1. One without path variable 一个没有路径变量的

      @RequestMapping(value = "create", method = RequestMethod.GET) public String create_form(Model model, @ModelAttribute("channelNode") ChannelNode channelNode, BindingResult result) { return CHANNELNODE_ADD_VIEW; } 
  2. One with path variable 一个带路径变量

     @RequestMapping(value = "create/{id}", method = RequestMethod.GET) public String create_form(@PathVariable(value = "id") Long id,Model model, @ModelAttribute("channelNode") ChannelNode channelNode, BindingResult result) { return CHANNELNODE_ADD_VIEW; } 

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

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