PDA

View Full Version : MVC: @RequestParam, HttpServletRequest ... Advantages/Disadvantages?



jeeper
Jan 17th, 2011, 05:06 AM
Hello,

Ive got one question. Im working with Spring Framework and Spring MVC. I have a formular, which is filled out by an user. Per click on a button it is sent to the controller which is responsible. Now I found out that there are different methods to receive the paramters which were sent. Im interessted in the advantages and disadvantages of these methods.

Possibility 1:


@RequestMapping(value ={"/user/page"}, method = RequestMethod.POST)
public ModelAndView doSomething(HttpServletRequest req) {
String parameter= req.getParameter("parametername");
....


Possibility 2:


@RequestMapping(value ={"/user/page"}, method = RequestMethod.POST)
public ModelAndView doSomething(@RequestParam String parametername) {
....


Possibility 3:


@RequestMapping(value ={"/user/page"}, method = RequestMethod.POST)
public ModelAndView doSomething(String parametername) {
....


Im not sure where the differences are.

1.
When I use HttpServletRequest I can get all parameters which were sent by requesting them. (getParameter()) The user might have not filled out all fields, but the Request is sent successfully. I guess when I use the other two possibilities, this would not be the case.

2. When I use possibility one or two, the parameters which are given to the method must ALL be sent. When I use @RequestParam, I can chose if a parameter is required or is not. So if the user doesn't fill out a field which is not necessarily required, this doesnt matter. But when I use possibility 3 he must fill out every field. The order of the parameters does not matter in any of those two cases.

These are the differences I could detect. Are they right and are there some more? Would you prefer one method? I guess possibilty one and two are useful. Possiblity 3 is difficult, because every parameter which is set must be sent.

I would like to hear your opinions. If there are any other possibilities, please tell me! :-)

Thank you!

jeeper
Jan 18th, 2011, 07:14 AM
would be great if someone would answer. maybe that question is too easy for you, but I would appreciate knowing your opinion about it. than you :-)

dbrinker
Jan 24th, 2011, 11:45 AM
There's a couple of points to make here:

Parameters annotated with @RequestParam can be optional i.e.


@RequestMapping(value ={"/user/page"}, method = RequestMethod.POST)
public ModelAndView doSomething(@RequestParam(required="false") String parametername) {
....


HOWEVER, if you have many form fields, you're much better off encapsulating all your fields into a single form object. Spring MVC will automatically convert the parameters with names matching the fields in question to attributes of the form object.



@RequestMapping(value ={"/user/page"}, method = RequestMethod.POST)
public ModelAndView doSomething(FormObject form) {
....


Presuming you're using Spring 3, you can use Bean Validation to automatically validate your form object



public class FormObject {
@NotNull
private String requiredField;

private String optionalField;

// Getters, setters, other attributes, etc.
}


In general, either of these approaches are preferred over using the Request directly, since they tend to hide a lot of boilerplate code (get the parameter, check if it's required, convert the type, etc).

As an unrelated aside, note that you can skip using the ModelAndView by including the Model as a parameter and returning the view name (as a String) i.e.


@RequestMapping(value ={"/user/page"}, method = RequestMethod.POST)
public String doSomething(Model model, @RequestParam String parametername) {
....



Hope this helps
- Don

jeeper
Jan 24th, 2011, 12:24 PM
wow, great hint, I did not know this! Thank you very much for this detailed and helpful answer! I will try now to implement the form object :-)