简体   繁体   English

Grails获取原始控制器的URL

[英]Grails get url of originating controller

I have a URL say abc.com/somecontroller?someparam=1 which renders a form. 我有一个说abc.com/somecontroller?someparam=1的URL,它呈现一个表单。 Form on submit sends the form params to /ajaxAction 提交表单时将表单参数发送到/ajaxAction

Is there a way I could get this abc.com/somecontroller?someparam=1 (ie the form URL?) 有没有办法我可以得到这个abc.com/somecontroller?someparam=1 (即表单URL?)

I am more interested in getting the someparam value from the URL of the form. 我对从表单的URL获取someparam值更感兴趣。

PS: the above URL abc.com/somecontroller?someparam=1 is dynamically generated, so I can not access it otherwise. PS:上面的URL abc.com/somecontroller?someparam=1是动态生成的,因此我无法访问它。 And request.forwardURI will give me /ajaxAction (ie the URL of the action in form and not the url of the form itself). 并且request.forwardURI将给我/ajaxAction (即,表单中操作的URL,而不是表单本身的URL)。

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. 因此,如果我更喜欢其他方法来获取URL。

Why don't you use javascript in form and add to ajax request array with GET params? 为什么不使用javascript形式并使用GET参数将其添加到ajax请求数组中? (or with the url of the action which generated form) (或带有生成表格的操作的网址)

You can get them from original request fe by this script. 您可以通过脚本从原始请求fe中获取它们。

While rendering the GSP of your form, you can do like this: 呈现表单的GSP时,您可以执行以下操作:

myaction.gsp 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 : 因此,当您渲染GSP时, script标签将具有类似以下内容(对于abc.com/somecontroller?someparam=1&foo=2 URL:

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

Now, in your AJAX code, you can pass that string as the query arguments: 现在,在您的AJAX代码中,您可以将该字符串作为查询参数传递:

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

Now, in your ajax controller action, you can simply do the params.someparam . 现在,在ajax控制器操作中,您只需执行params.someparam

EDIT 编辑

Well, I just realized that you don't have to any GSP stuff I mentioned above. 好吧,我只是意识到您不需要上面提到的任何GSP东西。 Simply do the AJAX call like this: 只需像这样进行AJAX调用:

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

The location.search will give you the direct string: ?someparam=1&foo=2 location.search将为您提供直接字符串: ?someparam=1&foo=2

I ended up using flash to store the someparam 我最终使用Flash来存储someparam

In my controller which is being used to render the form at abc.com/somecontroller?someparam=1 I use flash as this: 在我的用于在abc.com/somecontroller?someparam=1上呈现表单的abc.com/somecontroller?someparam=1我使用flash是这样的:

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. 我觉得这在我无法控制gsp的情况下会很好用。

If anyone finds any issue, please comment otherwise I will mark this as the answer. 如果有人发现任何问题,请发表评论,否则我会将其标记为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM