+-
java – Spring @Controller分离GET和POST映射
我正在使用 Spring MVC和注释配置.我有一个用于处理HTTP GET调用的控制器类:

@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
    }

并且用于处理表单的Controller在该URL上提交

@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中不受支持.我试图将控制器用于生成表单的原因与保存表单的控制器分开,因为我使用Springs @ExceptionHandler来处理发生的任何运行时异常,并且我想处理异常以显示视图而不是异常用于保存记录.

有没有不同的方法来处理我想要做的事情(对特定种类的请求使用Springs @ExceptionHandler注释?)

最佳答案

The reason I am trying to separate the controller for generating the form from the controller saving the form because I am using Springs @ExceptionHandler to handle any runtime exceptions that occur, and I would like to handle an exception for displaying the view differently than an exception for saving a record

我想你的视图模板引擎会抛出不同类型层次结构的异常,而不是在数据存储区中保存记录时遇到的异常.将这些方法放在同一个类中可能最容易,然后通过一种方式映射视图引擎类型的异常和另一种DB异常来解决@ExceptionResolver问题.

点击查看更多相关文章

转载注明原文:java – Spring @Controller分离GET和POST映射 - 乐贴网