我正在使用带有注解配置的 Spring MVC。我有一个用于处理 HTTP GET 调用的 Controller 类:
@Controller
@RequestMapping("/form")
public class FormController {
@RequestMapping(value = "/{table}/{identifier}/edit", method = RequestMethod.GET)
public ModelAndView getEditView(ModelMap map, @PathVariable String table, @PathVariable Object identifier) {
//generate the view for this record
}
以及用于处理在该 URL 上提交的表单的 Controller
@Controller
@RequestMapping("/form")
public class FormSaveController {
@RequestMapping(value = "/{table}/{identifier}/edit", method = RequestMethod.POST)
public ModelAndView saveView(WebRequest request, @PathVariable String table, @PathVariable Object identifier) {
//save the updated values and redirect to view
}
当我尝试启动我的容器时,spring 报错
Caused by: java.lang.IllegalStateException: Cannot map handler 'FormSaveController' to URL path [/form/{table}/{identifier}/edit]: There is already handler of type [class com.company.web.FormController] mapped.
这似乎表明我正在尝试做的事情在 Spring 中不受支持。我试图将用于生成表单的 Controller 与保存表单的 Controller 分开的原因是因为我正在使用 Springs @ExceptionHandler
来处理发生的任何运行时异常,并且我想处理一个异常以不同于保存记录的异常方式显示 View 。
是否有不同的方式来处理我正在尝试做的事情(针对特定类型的请求使用 Springs @ExceptionHandler
注释?)
最佳答案
您是否尝试过在同一个类(class)中使用?我认为那会起作用。如果您想使用 ExceptionHandler,请尝试 HandlerExceptionResolver
关于java - Spring @Controller 分离 GET 和 POST 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6099446/