简体   繁体   中英

Spring @Controller Separating GET and POST mappings

I am using Spring MVC with annotation configuration. I have a controller class for handling HTTP GET calls:

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

and a Controller for processing form submits on that 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
    }

When I attempt to startup my container, spring complains

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.

This seems to indicate what I'm trying to do is not supported in Spring. 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.

Is there a different way of handling what I am trying to do (utilize Springs @ExceptionHandler annotation for specific kinds of request?)

Have you tried using in the same Class? I think that would work. If you wish to use ExceptionHandler then try HandlerExceptionResolver

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

I would imagine that your view template engine would throw exceptions of a different type hierarchy than exceptions encountered while saving records in your datastore. It may be easiest to place these methods in the same class, and then just address your @ExceptionResolver concern by mapping exceptions of the view engine's type one way, and DB exceptions another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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