笔者最初的一套代码模板
import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Map; @Controller @Slf4j @RequestMapping("app/*") public class AppController { @RequestMapping("list") public String list(Model model) { return "open/app/list"; } @RequestMapping(name = "list", method = RequestMethod.POST) public String list(Model model, String keyword) { return "open/app/list"; } @RequestMapping("create") public String create(Model model) { return "open/app/editor"; } @RequestMapping(name = "view") public String view(Model model,Integer id) { return "open/app/editor"; } @RequestMapping(name = "save", method = RequestMethod.POST) @ResponseBody public Map<String,Object> saveApp() { return null; } @RequestMapping(name = "update" ,method = RequestMethod.POST) @ResponseBody public Map<String,Object> update() { return null; } }
注意标红加粗的地方。
然后又把这个文件复制了一遍重命名,为OrderController,然后就报错了。
最终发现原因是把@RequestMapping里面的参数填写错误,把name改成value
正确代码如下
import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Map; @Controller @Slf4j @RequestMapping("app/*") public class AppController { @RequestMapping("list") public String list(Model model) { return "open/app/list"; } @RequestMapping(value = "list", method = RequestMethod.POST) public String list(Model model, String keyword) { return "open/app/list"; } @RequestMapping("create") public String create(Model model) { return "open/app/editor"; } @RequestMapping(value = "view") public String view(Model model,Integer id) { return "open/app/editor"; } @RequestMapping(value = "save", method = RequestMethod.POST) @ResponseBody public Map<String,Object> saveApp() { return null; } @RequestMapping(value = "update" ,method = RequestMethod.POST) @ResponseBody public Map<String,Object> update() { return null; } }