1

I have a URL say abc.com/somecontroller?someparam=1 which renders a form. Form on submit sends the form params to /ajaxAction

Is there a way I could get this abc.com/somecontroller?someparam=1 (i.e. the form URL?)

I am more interested in getting the someparam value from the URL of the form.

PS: the above URL abc.com/somecontroller?someparam=1 is dynamically generated, so I can not access it otherwise. And request.forwardURI will give me /ajaxAction (i.e. the URL of the action in form and not the url of the form itself).

EDIT: I have no control over form as it is also dynamic and user has hundreds of templates to select from. Each template has different no. of fields. So if I would prefer some other way to get the URL.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
rahulserver
  • 10,411
  • 24
  • 90
  • 164

3 Answers3

1

Why don't you use javascript in form and add to ajax request array with GET params? (or with the url of the action which generated form)

You can get them from original request f.e. by this script.

Community
  • 1
  • 1
Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59
0

While rendering the GSP of your form, you can do like this:

myaction.gsp

<html>
<body>
<!-- Your content -->

<script>
    var paramsString = '${params.collect { k, v-> "$k=$v" }.join​​​​​​​​​​​​​​​​​​​​​​("&")​​​ }';
</script>
</body>
</html>

So, when you GSP is rendered, the script tag will have something like this (for a URL abc.com/somecontroller?someparam=1&foo=2:

var paramsString = 'someparam=1&foo=2';

Now, in your AJAX code, you can pass that string as the query arguments:

$.ajax({
    url: '/ajaxAction?' + paramsString
    /* rest of the AJAX code */
});

Now, in your ajax controller action, you can simply do the params.someparam.

EDIT

Well, I just realized that you don't have to any GSP stuff I mentioned above. Simply do the AJAX call like this:

$.ajax({
    url: '/ajaxAction' + location.search
    /* rest of the AJAX code */
});

The location.search will give you the direct string: ?someparam=1&foo=2

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
0

I ended up using flash to store the someparam

In my controller which is being used to render the form at abc.com/somecontroller?someparam=1 I use flash as this:

flash.put("someparam", params.someparam)

This worked as a quick workaround to the issue. I feel this would work well in situations where I have no control over the gsp.

If anyone finds any issue, please comment otherwise I will mark this as the answer.

rahulserver
  • 10,411
  • 24
  • 90
  • 164