views:

844

answers:

4

With Spring MVC, it's easy to express a concept like "A user is submitting the form if they use POST or if they include the 'isSubmit' parameter." You'd just extend SimpleFormController and override the isFormSubmission method.

However, Spring MVC now uses these neat annotations like @RequestMapping to handle requests. @RequestMapping has an obvious filter for whether somebody used a GET or a POST, but I don't see any inherent support for all of the useful logic SimpleFormController provided. Is it still available to me with annotations?

A: 

Copying from here:

Path mappings can be narrowed through parameter conditions: a sequence of 
"myParam=myValue" style expressions, with a request only mapped if each such 
parameter is found to have the given value. "myParam" style expressions are 
also supported, with such parameters having to be present in the request
(allowed to have any value). Finally, "!myParam" style expressions indicate 
that the specified parameter is not supposed to be present in the request.

You can only use the RequestMapping options to define the desired functionality. The Annotations Controller doesn't implement any interface to work with.

kgiannakakis
But that only works to further narrow the request. I can't say "it's a post OR it has this parameter." I can only say "It's a post AND it has this parameter".
CaptainAwesomePants
You could have two different methods calling a private submitForm method. The first will use a RequestMapping to handle the POST case, the other one the GET and the parameter.
kgiannakakis
+1 for that. The form submission handler method couild be called by a variety of annotated handler methods, each of which catches the variety of ways to express a form submission (e.g. one which catches a POST, another which catches the one with the parameter). It's not pretty, but then what you're trying to do is highly peculiar and not really a good idea to begin with.
skaffman
+1  A: 

So, after a bit of investigation, there are in fact a couple of ways to handle this situation.

The first way is to go ahead and use a SimpleFormController with with @RequestMapping annotation at the class level. A lesser-known but pretty cool property of @RequestMapping is that it knows perfectly well how to deal with the classes that implement Spring's Controller interface. The only downside here is that I'm still using the old MVC interfaces and classes, and they're going to be deprecated in Spring 3.0.

The second way was pointed out by kgiannakakis above. Simply create a @RequestMapping method for every way that the submit can be called, and have them all just call a single submit method, either in a constructor-chaining style or with some private method. Simple and easy to understand. Thanks, kgiannakakis!

CaptainAwesomePants
A: 

Here is one example of using Path mappings:

    @RequestMapping(params = "formAction=APPROVE", method = RequestMethod.POST)
    public String myMethod ()....

This method will only be called then for POSTs where there is a field named "formAction" with a value of "APPROVE".

B Shelton
A: 

The other answers listed work fine for a method annotated with @RequestMapping.

However, if you want to achieve the same thing on a method annotated with @InitBinder, you can simply do this:

@InitBinder
public void initBinder(HttpServletRequest request) {
    if ("POST".equals(request.getMethod()){
        //Do something
    }
}
Daniel Alexiuc