Scott
Scott

Reputation: 9488

Spring MVC Annotated Controller

I have a controller which has a few actions, which are triggered by hitting various buttons on the page. I would like to have a default action, but am unsure how to annotate the method. Here is an example:

@Controller
@RequestMapping("/view.jsp")
public class ExampleController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView displayResults() {
        ModelAndView mav = new ModelAndView("view");
        mav.addObject("queryResults", methodToQueryDBForListing());
        return mav;
    }

    @RequestMapping(method = RequestMethod.POST, params="submit=Action 1")
    public ModelAndView action1(@RequestParam("selectedItemKey") String key) {
        ModelAndView mav = new ModelAndView("action1");
        //Business logic
        return mav;
    }

    @RequestMapping(method = RequestMethod.POST, params="submit=Action 2")
    public ModelAndView action2(@RequestParam("selectedItemKey") String key) {
        ModelAndView mav = new ModelAndView("action2");
        //Business logic
        return mav;
    }

    //A few more methods which basically do what action1 and action2 do
}

How can I annotate a method which will act on a POST with any submit button pressed with no key selected?

I have tried:

@RequestMethod(method = RequestMethod.POST, params="!selectedItemKey")
@RequestMethod(method = RequestMethod.POST)

I'd really hate it if I had to set required = false on each of the methods which take RequestParams and then conditionally check to see if one comes in or not... Is there a way to annotate this to work properly?

Upvotes: 0

Views: 3633

Answers (3)

Scott
Scott

Reputation: 9488

The proper annotation is:

@RequestMapping(
  method = RequestMethod.POST, 
  params = { "!selectedItemKey", "submit" }
)     

It seems odd though, that it was not hitting this method until adding that second parameter.

Upvotes: 1

dfme
dfme

Reputation: 291

I'm not so familiar with annotated spring-mvc but I can remember that when extending a MultiActionController you can specify a default entry point by defining the following spring config:

<bean name="myController"
class="foo.bar.MyController">
<property name="methodNameResolver">
    <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="defaultMethodName" value="init" />
    </bean>
</property>


package foo.bar

public class MyController extends MultiActionController {

    /**
     * Called when no parameter was passed.
     */
    public ModelAndView init(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("myDefaultView");
    }

    /**
     * action=method1
     */
    public void method1(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("method1"); 
    }

    /**
     * action=method2
     */
    public void method2(HttpServletRequest request,
            HttpServletResponse response) {
        return new ModelAndView("method2"); 
    }
}

So maybe in this instance you could solve this by configuring your controller over spring config instead of using annotations.

Upvotes: 0

Bozho
Bozho

Reputation: 597382

I would make this a path variable rather than a parameter, and would get rid of the .jsp:

@RequestMapping("/view")
...

@RequestMapping("/action1")
@RequestMapping("/action2")
@RequestMapping("/{action}")

It is more "restful".

Upvotes: 3

Related Questions