Results 1 to 9 of 9

Thread: @RequestParam Annotation doesnt work for Post Parameters?

  1. #1
    Join Date
    Jan 2012
    Posts
    4

    Default @RequestParam Annotation doesnt work for Post Parameters?

    Hi,

    I am using @RequestParams to get the values of the parameters passed in the querystring.When I added some parameters in the Post body with name and value,the @RequestParams doesnt work.It shows null for the parameter name.

    With @RequestBody I am able to see the parameter name and value as a string but not able to access those using @RequestParams.

    Whether @RequestParams doesnt work for parameters passed in post request body?

    Any help on this is appreciated

    Regards

  2. #2

    Default

    Can you post your code.

    Also ensure you put it in code quotes

  3. #3
    Join Date
    Jan 2012
    Posts
    4

    Default

    Hi,

    I am posting only the method snippet here.

    private static final String LAST_ROW_ID = "last_row_id"; //declared as class variables
    private static final String COLUMNS = "columns";//declared as class variables

    @RequestMapping(value = { "/new/setting/{set_id}"} ,method = RequestMethod.POST)
    public ResponseEntity<String> setPOST(
    @PathVariable Long set_id,
    @RequestParam(value = COLUMNS, required = false) String columns,
    @RequestParam(value = LAST_ROW_ID, required = false) String lastRowId,
    {
    Here the URL I hit is

    http://localhost:8080/new/setting/15245 and the POST BODY has the param name and value

    last_row_id=2590&columns=testColumn

    The @RequestParam doesnt fetch this name and value.

    But if I sent the parameters in the query string in the POST request it is working.

    for example

    http://localhost:8080/new/setting/15...mns=testColumn


    Why the same when passed in the POST body doesnt work?If I use @RequestBody annotation I get the value as a string.

    What could have gone wrong?

    Any help on this is appreciated.

    Regards

  4. #4
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    This is close to a question I am going to post in this forum.

    But an html <form> post will take the form values and make a string like you posted. They will be sent as request parameters in the Request object. My post will ask how to get the form values in as json in the RequestBody. But I have been spending the past few weeks doing this, and I can only get things like @RequestParam or @ModelAttribute to work. With @ModelAttribute if you have a domain object with setters that match the param names it can create an instance of that domain object and populate their values directly.

    In my code though I post my forms through Ajax/JQuery .post() function. and use form.serialize() method to serialize the form values and post them. These definitely got sent as RequestParams.

    Also, if you use Spring's form tags and add a backing domain object, then you can just get the domain object to be passed in as a parameter to your controller method.

    As an example wihtout Spring form tags, this is my registration page and code

    my registration jsp page
    Code:
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/eventgate/forms/submitRegistration.js"></script>
    <form id="registrationForm" class="popupForm">
        <div class="line">
        	<p>Please Register</p>
        </div>
        <div class="line">
            <label for="login">User Name</label>
            <input id="login" type="text" name="login" placeholder="User Name">
        </div>
        <div class="line">
            <label for="password">Password</label>
            <input id="password" type="password" name="password" placeholder="Password">
        </div>
        <div class="line">
            <label for="firstName">First Name</label>
            <input id="firstName" type="text" name="firstName" placeholder="First Name">
        </div>
        <div class="line">
            <label for="lastName">Last Name</label>
            <input id="lastName" type="text" name="lastName" placeholder="Last Name">
        </div>
        <div class="line radius6">
            <a id="submitRegistration" class="button large" href="javascript:parent.$.fancybox.close();">Submit Registration</a>
        </div>
    </form>
    My javascript for the page

    Code:
    $('#submitRegistration').click(function(){
        var login = document.getElementById('login').value;
        var firstName = document.getElementById('firstName').value;
        var lastName = document.getElementById('lastName').value;
    
        var controllerPath = "/users";
        alert($("#registrationForm").serialize());
        $.post(controllerPath, $("#registrationForm").serialize(),
            function(){
                //afterLogin(data);
                showAlert('Thank you ' + firstName + ' ' + lastName + ' for registering with EventGate. Your UserName for logging in is ' + login + '. No verification is required.');
            }
        ).error(function() { showAlert("Registration Failed"); });
    });
    And finally my controller method. My controller is mapped to "/users" so the requestMapping on the method includes that from the class level annotation
    Code:
    @RequestMapping(method = RequestMethod.POST)
        @ResponseStatus(HttpStatus.CREATED)
        public void register(@RequestParam() String login,
                             @RequestParam() String password,
                             @RequestParam() String firstName,
                             @RequestParam() String lastName) {
            User user = new User(login, password, firstName, lastName);
            userService.register(user);
        }
    So in my case the values from the form get submitted and passing in as @RequestParam

    Something similar I have in jsp and javascript, but my code is different is this method

    Code:
    @RequestMapping(value="/support", method = RequestMethod.POST)
        @ResponseStatus(HttpStatus.OK)
        public void support(Support support) {
            System.out.println(support);
        }
    In that case the support.jsp and javascript have the same type of code for registration, but in my method I don't annotate it at all for the parameter and Spring automatically converted the request params into a Support object because the Support object has

    Code:
    public class Support {
        private String firstName;
        private String lastName;
        private String email;
        private String comment;
    
        //getters and setters
    }
    Hope that helps. and shows that it does work.

    Mark

  5. #5

    Default

    @Haddock

    Do you have a form bound as a command object? Can you supply more code, specifically your jsp as well as the GET and POST methods of your controller.

  6. #6
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    No, I do not have a command object.

    As far as supplying more code, I posted all my code. The html you see is my jsp file. the @RequestMapping code is my post method. I do not have a get as I don't have to get anything to display the page. I mean in my case, the .jsp is included in my index.jsp so that is already on the client side. But I can create a GET method in my controller, but it would only need to return a string for the view if I had to get the .jsp rendered. If you create a controller method that just returns a string for view resolution, you can use <mvc:controller> instead and bypass having to write code for that scenario.

    So there isn't any more code for me to post. I had already posted all my code.

    Mark

  7. #7
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    Quote Originally Posted by knoxor View Post
    @Haddock

    Do you have a form bound as a command object? Can you supply more code, specifically your jsp as well as the GET and POST methods of your controller.
    That is funny. I just responded thinking that this was from the original poster asking for more code from me, instead of someone else trying to help asking the original poster for their code.

    Mark

  8. #8

    Default

    Hi Mark,

    Yeah, I wasn't sure what you were saying there then I realised you thought I was talking to you.

    As to your own question, I haven't used json or the @RequestBody annotation before so can't help you on that one.

    Is there a requirement to use this method to submit forms?
    I'd normally just use the spring tag libs and the @ModelAttribute as parameters and POST the form which is bound to the @ModelAttribute object.

    Paul

  9. #9
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    I can't use the Spring tag libs because all my forms are in the html in the first request for the index.html then everything stays on the client-side. It is a single page app. So all requests after the first one are all ajax/REST based.

    In a couple of cases I just have my REST method take a domain object and don't even need an annotation like @ModelAttribute as Spring with <mvc:annotation-driven/> automatically includes a converter to take the request params and match them to the domain objects properties directly.

    For example my support form posts to

    Code:
    @RequestMapping(value="/support", method = RequestMethod.POST)
        @ResponseStatus(HttpStatus.OK)
        public void support(Support support) {
            System.out.println(support);
        }
    the jquery/javascript code is

    Code:
    var controllerPath = "/users/support";
        $.post(controllerPath, $("#supportForm").serializeJSON(),
            function(){
            }
        ).error(function() { showAlert("Support submission Failed"); }
        ).complete(function() {
            showAlert('Thank you ' + firstname + ' ' + lastname + ' for contacting us. We should reply within 48 hours of your request');
        });
    While it serializes the form into JSON, the data is still passed as Request parameters. There is a jquery form plugin and that might do what I need.

    But, I digress and am hijacking this thread.

    Mark

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
  •