No way to pass variables for reverse url mappings to remoteFunction - grails

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

Related

how to send data from one grails application to other grails application

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.

Apache Camel exchange body to URI query parameters

I have an apache camel route that is making an HTTP POST request i.e.
from(...).setHeader(Exchange.HTTP_METHOD, constant(POST)).to("http4://myUrl?...");
The request URL includes query params (unusual for POST i know, but I have no choice here) that I need to populate from details of the exchange which are stored on the body.
i.e. The body is a POJO like so:
public class Params {
String param1;
int param2;
....
//etc. etc. including getters and setters
}
Where each field is either a primitive or string, and refers directly to an equivalent query parameter:
http4://myUrl?param1=...&param2=...&...
Is there a way I can avoid having to manually define every parameter on the URL and instead automagically map the exchange body to query params on the request being made?
The reason I need to do this is that some of the query params are optional, and should be populated based on the contents of the exchange body.
You can set the header Exchange.HTTP_QUERY with the query parameters separated.
And its not possible to automatic map from a message bodies its fields to URI parameters (no magic included). You would need to build some code that computes the URI query with & separating the values, and setting that as the HTTP_QUERY header.

Grails Reverse Url Mapping: Is there a way to build links based on the currently matched route?

I'm trying to build some dynamic URL mappings that are based on the domain classes of the grails project. The goal is to use a single generic controller, but the URL decides which domain is being used for processing. The problem is now, that I don't get the desired URLs when using the <g:link /> tag.
I tried the following variants to create the URL mappings:
static mappings = {
Holders.grailsApplication.domainClasses.each {
"/admin/${it.propertyName}/$action?/$id?"(controller: "admin")
}
}
static mappings = {
"/admin/$domainClass/$action?/$id?"(controller: "admin")
}
Both variants work for the actual URL matching. But I personally don't like the behavior of the reverse URL mapping by grails. In case of variant 1, the reverse mapping always resolves to the last added URL mapping for the AdminController. For case 2 I have the problem that I would have to pass the domainClass-Parameter to every link-creating call, even though it is theoretically not necessary, since the information is already present in the current request.
I know there is the possibility of using named URL mappings and then using something like <g:link mapping="mapping_name" />. The problem is that I am using some generic application-wide partial-views, where I try to only provide the necessary information for creating a link, like <g:link action="something" />.
This leads to my two questions:
Is there a possibility to get g:link to build the URL based on the matched mapping in the current request?
Is there a way to get a reference to the mapping that matched the current request, so I can implement to desired behavior myself?
You could define named mappings like
Holders.grailsApplication.domainClasses.each { dc ->
name((dc.propertyName):"/admin/${dc.propertyName}/$action?/$id?" {
controller = "admin"
domainClass = dc.propertyName
})
}
With the mapping name saved in the params you can now do
<g:link mapping="${params.domainClass}">link text</g:link>

Appending ?param= to mvc routes

Some MVC sites have querystring params appended to the route Url (of which I noticed StackOverflow does), such as:
https://stackoverflow.com/questions/tagged/java?page=9802&sort=newest&pagesize=15
What are the advantages of having the parameters as more conventional ?querystring params, rather than /param/values/ ?
Also, how are these params appended to routes that have been set up? I'm familiar with setting up mvc routes with params like "users/details/{id}" etc. but don't know how to configure routes for use with 1 or more ?params as per the example url above?
Query string parameters are useful when you have multiple optional parameters and don't want to include default values for non-specified parameters just to satisfy a path.
And you don't have to do anything special to include these parameters in a rendered URL.
Take the following route for example:
routes.MapRoute
(
"QuestionsTagged",
"questions/tagged/{tag}",
new { controller = "Questions", action = "Tagged" }
);
If you render a link to that route using:
Url.RouteUrl
(
"QuestionsTagged",
new
{
tag = "java",
page = 9802,
sort = "newest",
pagesize = 15
}
)
...then the routing engine is smart enough to see that the route contains a parameter named tag and that the passed route values object also has something named tag so it uses that value in the route.
Any provided route values that don't have corresponding parameters in the route (page, sort and pagesize in this case) get tacked on as query string parameters. So the Url.RouteUrl call above would return /questions/tagged/java?page=9802&sort=newest&pagesize=15.
And your action method can explicitly list these parameters in its signature (promotes readability and maintainability) or you can access them via Request.QueryString.
public class QuestionsController : Controller
{
// I can explicitly list the parameters in my signature and let routing do
// its magic, like this...
public ViewResult Tagged(string tag, int? page, int? pagesize)
{
// ...or I can grab parameters like this:
string sort = Request.QueryString["sort"];
return View();
}
}
Note that the parameters to the action method do not have to match the parameters specified in the route. (In the route, I only specified tag, but the action method's signature lists tag, page, and pagesize.) However, any parameter of the action method that is not also a parameter of the route must be a reference or nullable type.
I've normally seen paging and filtering data be passed as querystring parameters since it gives information to the user in the URI. It is also normally harmless if a user alters this data since it will just filter the data you see on the page. Any sensitive data is normally posted so as it is not as easily seen or modified, but I would argue to keep your URI's clean and use quesrystrings as little as possible.
You don't need to do anything special when specifying routes to be able to handle quesrystrings. They will just be extra data that is passed to your action. On your action you will need to do some work to handle the data though. Using your querystring above you will have to specify the querystring names as the parameter names and then whatever datatype you are expecting.
public ActionResult Index (int page, string sort, int pagesize)
In this example, page will be the value of 9802, sort will be "newest" and pagesize will be 15.

How do you use querystrings with ASP.NET routing?

The new ASP.NET routing is great for simple path style URL's but if you want to use a url such as:
http://example.com/items/search.xhtml?term=Text+to+find&page=2
Do you have to use a catch all parameter with a validation?
You can match querystring parameters with routes as well, if you want to just capture everything you need to add a parameter like so:
{*contentUrl}
Which will populate the rest of the url into that variable.
Any view data items that are not listed in the route are automatically mapped to the querystring, so if you map "items/search.xhtml" to an action:
Search(string term, int page)
Then you should get the results you are looking for.
I was also having trouble passing an encoded URL to a route as a route parameter.
You can't use url encoded chars in a URL, but you can in a query string.
Therefore I needed my route to also have a query string element to it.
Say I have a route:
MapPageRoute("myroute", "myroute/{x}", "~/routehander.aspx")
But I want it in the form of:
http://mywebsite.com/myroute/{x}?url=myurl
We can do this:
Dim x as integer = 12
Dim rvd As New Routing.RouteValueDictionary
rvd.Add("x", x)
rvd.Add("url", Server.UrlEncode("/default.aspx"))
HttpContext.Current.ApplicationInstance.Response.RedirectToRoutePermanent("myroute", rvd)
This would redirect us to the following url:
http://mywebsite.com/myroute/12?url=%252fdefault.aspx
You can still use Request.QueryString["some_value"];

Resources