0

I have a form that once submitted send requests like following

Form

       <form:form action="/edit" modelAttribute="edit" role="form" 
       method="GET">

Requests

/edit?param1=1&param2=&param3=
/edit?param1=1&param2=this & that$param3=
/edit?param1=1&param2=2&param3=3

I am doing as following but it does not work

@Controller
@RequestMapping("edit")
public class Edit{
   ....
   @RequestMapping(value = "?param1={p1}&param2=&param3=", method = RequestMethod.GET)

*Please note param names might include &

Jack
  • 6,430
  • 27
  • 80
  • 151
  • this might be of some help to you http://stackoverflow.com/questions/13213061/springmvc-requestmapping-for-get-parameters – Vihar May 02 '15 at 04:39

1 Answers1

0

You can get a lot of flexibility by adding a @RequestParam Map<String, String> all parameter e.g.

@RequestMapping(@RequestParam Map<String, String> all, method = RequestMethod.GET)

this way all your parameters will be bound to the map all, where key will be the names of your parameter and value will be the value of the param


Alternatively, you can add a form backing bean, especially as you already have the modelAttribute=edit e.g.

@RequestMapping(FormBean edit, method = RequestMethod.GET)

In your FormBean you can list all the possible parameter names as properties and they will get properly bound e.g.

public class FormBean {
    private String param1;

    public String getParam1() {
        return param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }
}  

Update after comment

you can also list all the possible parameters, and mark them as required=false, e.g.

@RequestMapping(@RequestParam(required = false, value = "param1") String param, @RequestParam(required = false, value = "param2") String param,method = RequestMethod.GET)

Note one thing, server will always use the existence of the & char in a query string as a parameter delimeter, that is why you can't have it as a parameter name

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • thanks, does that mean I would have one method only and need to have if condition / map contains function to find out which ones are passed? is not it possible to have separate method for each format of request? – Jack May 02 '15 at 05:57
  • Yes, you would have to go through the map list. I initially thought that you're looking for a way to map a dynamic set of params, but it is possible to have a separate method for each request, just list the param in `@RequestParam` and set the `required` flag accordingly, I've updated the answer with the example – Master Slave May 02 '15 at 11:13