我正在使用注解配置的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
}
还需要一个控制器来处理该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中不受支持。我尝试将生成表单的控制器与保存表单的控制器分开,因为我使用Spring的@ExceptionHandler
来处理任何运行时异常,并且我希望处理用于显示视图的异常与用于保存记录的异常不同。
是否有一种不同的方式来处理我正在尝试做的事情(利用Spring的@ExceptionHandler
注释来处理特定类型的请求?)
PersistenceExceptionTranslationPostProcessor
将所有与数据访问相关的异常转换为DataAccessException
的子异常。因此,现在我有一些用于自定义异常的@ExceptionHandler
方法,以及一个用于数据访问相关异常(例如在数据库中插入/更新记录)的方法。 - BuffaloBuffalo