I have created 2 application in grails both are interconnected, now I need to pass data from one application to other, how can i pass using redirect url
redirect(url:'url path')
Well you can pass your data to controller action pretty easily with use of params
Specify the input parameters like below
redirect(uri: "url path", params: [content : "your_data"])
You can pass more than one parameter just add that map to params like below
redirect(uri: "url path", params: [content : "your_data", other_content: "some_other_data"])
Please modify the key value as per your need.
Related
In an MVC project, I have a partial
#Html.Partial("_MyPartial");
which calls a controller method (using AJAX with jQuery) via URL
'/Home/MyActionMethod?type=myType&sort=az&page=1'
The Home controller contains MyActionMethod, which returns a PartialViewResult.
MyActionMethod has parameters matching those in the URL, which are mapped as per usual.
My question is how to access the top level querystring from MyActionMethod, because I want to include extra values in there? By top level I mean the one seen in the browser, as opposed to the one sent to MyActionMethod from _MyPartial
Many thanks
You have to include them as query string parameters when performing your AJAX call. So when building the link on the server, read the query string parameters and append them to those that you have already.
Let say I have a url mappings
"blog/$year/$month/$day" (controller:'blog')
There's no way that I can pass the values for $year, $month and $day to remoteFunction so that it generates the proper url.
without variables, default rule would match and generated url would be like "/controller/action" I know i can pass the params to remoteFunction - so incoming url will match the rule, but how about reverse url mappings - that is used to generate the url for the ajax endpoint.
Any solution ?
If you are looking for a way to put the parameters from the url mapping into a remoteFunction() call, the grails manual says this can be done by accessing the params map in the controller after the request is received:
static mappings = {
"/$blog/$year/$month/$day/$id"(controller: "blog", action: "show")
}
"The individual tokens in the URL would again be mapped into the
params object with values available for year, month, day, id and so
on."
http://grails.org/doc/latest/guide/theWebLayer.html#urlmappings
I'm trying to pass a list of URL's with Id attributes from a controller to a view.
I can pass a <a href=...> link back but I don't think writing a 'localhost' absolute path is a clean way of approaching this. I cant pass an ActionLink back as it returns the full string. Is ther a simple solution to this problem? Thanks in advance.
Using this overload of the UrlHelper.Action() method and Request object you can get a complete URL including the route parameters such as IDs and the actual hostname of the application.
string url = Url.Action("action", "controller",
new System.Web.Routing.RouteValueDictionary(new { id = id }),
"http", Request.Url.Host);
UrlHelper is available in the controller via its Url property.
You can then pass such URL into your view.
It is also possible to use UrlHelper directly inside your view to create URLs for controller actions. Depends if you really need to create them inside the controller.
Edit in response to comments:
Wherever you need to place the URLs, this "URL builder" you are looking for is still the UrlHelper. You just need to pass it (or the generated URLs) where you need it, being it inside the controller, view or custom helper.
To get the links inside the unsorted list HTML structure you mention, you need to put anchors inside the list items like this:
<ul>
<li>Link</li>
...
</ul>
Then again you just need to get the URLs from somewhere and that would be from UrlHelper.
Simple and easy.
text
the route id = the parameter that is going to be inserted into your method.
eg.
function Details(int id) {
//id has the value of my_var_id
}
So for instance, say i have an API on a webapp, and i wish to use the same controllers and actions in the API as the rest of the webapp.
In my urlmappings file i have
"/api/$version/$apiKey/$controller/$acion/$id?"
and i also have a mapping like this:
"/blog/$year/$month/$day/$action" {
controller = 'blog'
}
Now the question is, can i somehow prefix the api urlmapping to the blog urlmapping so i can benefit from the $year, $month, $day variables? in such a way that a GET request to the following url would be valid:
GET /api/0.1/bs23mk4m2n4k/blog/2001/01/05/list
or am i forced to do the following request instead?
GET /api/0.1/bs23mk4m2n4k/blog/list?year=2004&month=01&day=05
Need help from an urlmappings GURU or a groovy runtime urlmappings maniuplation WIZARD :)
I want a solution that can reuse existing non-api urmappings, instead of having to redeclare them with the api path as a prefix.
You could have an ApiController strip off the api parameters, then redirect to the blog controller. For example:
"/api/$version/$apiKey/$rest**" {
controller:'api'
action:'default'
}
import org.codehaus.groovy.grails.web.util.WebUtils
class ApiController {
def grailsUrlMappingsHolder
def default = {
// validate apiKey, etc
WebUtils.forwardRequestForUrlMappingInfo(request, response, grailsUrlMappingsHolder.match("/${params.rest}"))
}
}
The API controller has access to the version and apiKey params, and passes on the rest of the params to be processed by the blog controller's UrlMapping.
I think you want to use embedded variables. Check the url mapping reference: http://grails.org/doc/latest/guide/single.html#6.4 URL Mappings
just add the following mapping:
static mappings = {
"/api/$version/$apiKey/$controller/$year/$month/$day/$action"()
}
now you can use this url for example:
http://localhost:8080/api/0.1/bs23mk4m2n4k/blog/2001/01/05/list
now you get redirected to the list action in the blog controller.
there you can use params to show the parameters from the url (as defined in the mapping).
ex.
params.version
I had somewhat the same problem, solved it using named url mapping: http://www.grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.4.9%20Named%20URL%20Mappings
Hope this helps!
in urlMapping:
name blogWithYear:"/api/$version/$apiKey/$controller/$year/$month/$day":{
controller = 'blog'
action = 'youraction'
}
<g:link mapping="blogWithYear" params="[$version:'0.1', ....., '$year: 2011']">
Show blog
</g:link>
With g:link you can now compile the url however you want, adding parameters.
Recently, I'm trying to migrating my application from CakePHP to Grails. So far it's been a smooth sailing, everything I can do with CakePHP, I can do it with much less code in Grails. However, I have one question :
In CakePHP, there's an URL Prefix feature that enables you to give prefix to a certain action url, for example, if I have these actions in my controller :
PostController
admin_add
admin_edit
admin_delete
I can simply access it from the URL :
mysite/admin/post/add
mysite/admin/post/edit/1
mysite/admin/post/delete/2
instead of:
mysite/post/admin_add
mysite/post/admin_edit/1
mysite/post/admin_delete/2
Is there anyway to do this in Grails, or at least alternative of doing this?
Grails URL Mappings documentation doesn't help you in this particular case (amra, next time try it yourself and post an answer only if it's any help). Daniel's solution was close, but wouldn't work, because:
the action part must be in a closure when created dynamically
all named parameters excluding "controller", "action" and "id" are accessible via the params object
A solution could look like this:
"/admin/$controller/$adminAction?/$param?"{
action = { "admin_${params.adminAction}" }
}
The key is NOT to name the parameter as "action", because it seems to be directly mapped to an action and can not be overriden.
I also tried a dynamic solution with generic prefixes and it seems to work as well:
"/$prefix/$controller/$adminAction?/$param?"{
action = { "${params.prefix}_${params.adminAction}" }
}
I didn't test it, but try this:
"mysite/$prefix/$controller/$method/$id?"{
action = "${prefix}_${method}"
}
It constructs the action name from the prefix and the method.
Just take a look on grails URL Mappings documentation part