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!
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!