Results 1 to 7 of 7

Thread: How to get request parameter

  1. #1
    Join Date
    Oct 2010
    Posts
    13

    Default How to get request parameters with POST method

    Hi all,
    I'm currently doing some migration for a Spring 2.5.6 application to Spring 3.0.4 and I was wondering how I am supposed to get a parameter selected by a user on a page.

    With Spring 2.5.6, the code looked like that :
    Code:
    @Controller
    public class LoginController extends CustomController {
    	
    	private TechnoService technoService = null;
    	
    	// Other functions
    
    	@RequestMapping("/page1.wss")
    	public ModelAndView login(HttpSession session, HttpServletRequest request) {	
    		String technoID = request.getParameter("foobar");
                          // doing some weird things with the parameter ;)
    		String nextUrl = "/showWelcome.wss";
    		return new ModelAndView("redirect:" + nextUrl); 
    	} 
    
    }
    But running this code with the new framework doesnt work quite well.
    I've tried with the @RequestParam("foobar") in the function signature, but I get a runtime error saying that the parameter doesn't exist.

    Any thoughts about what's going on and how I could solve it?

    Thanks
    Last edited by mireaulf; Oct 15th, 2010 at 01:49 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    By default parameters are required, set it to optional if it isn't always there...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2010
    Posts
    13

    Default

    Well, I know that, I've read some of the documentation.

    What seems to happen is that the parameter IS NOT there but IT SHOULD be, any thoughts on what could be the cause of that?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If you can get it with request.getParameter you can also get it with @RequestParam, try including the name in the @RequestParam else it takes the method attribute name for lookup and if you compile without debug info that might not be available.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Oct 2010
    Posts
    13

    Default

    I think we misunderstood each others. The code I posted doesn't work because after this line

    Code:
    String technoID = request.getParameter("foobar");
    the variable is null, which means that it wasn't retrieved by the function.
    What could possibly cause this to happen, if it was working with the earlier version of Spring and doesn't work now with Spring 3.0.4.

    Also, using the @RequestParam doesn't work either. If I put it in the method signature as required (it is, I need this parameter to continue), I get the runtime error describe above.

    Thanks

    Edit: If it can help, the from submitting these data is using a POST method.
    Last edited by mireaulf; Oct 15th, 2010 at 01:51 PM.

  6. #6

    Default

    Edit: If it can help, the from submitting these data is using a POST method.
    If you are using the POST method, then the @RequestMapping annotation should include that. GET is the default HTTP method for @RequestMapping.

    Code:
    @RequestMapping(value="/page1.wss", method="POST")
    This was also the case with Spring 2.5, so I'm not sure how this was working before.

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I doubt it has something to do with spring, the request is basically the request from the app server. If you parameter is tehre (be it a GET or POST parameter) it should be retrievable, if not I suspect something else is wrong.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •