How to get absolute path to Grails application - grails

In my application, a login controller should return redirect URL if client is authorized:
<path to grails app> + <another controller, method, params>
How can I get application URL?

You should be able to build a URL using the grails tags as method calls. createLink is likely what you want. This has action, controller, params and absolute as options to get a full URL to a controller action (you may need to configure the base url as noted in the docs). In an example in the first link, something like this would get the absolute URL to that resource:
g.createLink(action:"myact", controller:"somecontroller", params:[foo:'bar', boo:'far'], absolute:true)

For generic approach you can inject LinkGenerator and then call
linkGenerator.link(controller: "foo", action: "bar", absolute: true)
For some reason, tag use completely different logic.

Related

How to access currently requested URL in a Grails GSP file

If I have a simple GSP (using Grails 2.3.6):
<% page import=org.me.Widget %>
<header>
<!-- Header stuff -->
</header>
<body>
The requested URL is ${url}!
</body>
How do I inspect the HTTP request URL (the current URL the server is responding to) and inject it into the ${url} variable?
Note: I need to do this inside of GSP and not from inside a controller, if at all humanly possible, even if it is a violation of best practices.
you could try
def var = request.forwardURI
forwardURI - Useful for obtaining the current request URI since the request object’s requestURI property returns the original URI, not the matched one.
You can even check for additional methods added to the request object here
You have quite a few options in a GSP to find out the current URL.
First, you can always access the request object which is an HttpServletRequest. Using methods such as getRequestURL() or getRequestURI(), you can inspect the request.
${request.getRequestURL()}
However, actionName and controllerName may be more useful. Both of these properties are exposed to the GSP within the model. With these you can construct the URL current using the standard createLink tag if needed.
${createLink(controller: controllerName, action: actionName)}
These should give you enough options to accomplish what you need.

Grails hyphenated URL converter issue with default index method

According to the Grails documentation, the default URL mapping (between controller actions and URLs) uses camel case. You can easily change the URL mapping to use hyphenated URLs:
grails.web.url.converter = 'hyphenated'
So for instance, HelloWorldController.showUsers would map to:
/hello-world/show-users
In Grails, you can have a default controller action which by convention is "index". So for instance, if you have a method named index(), the following URL will hit that method:
/hello-world
You can create an anchor tag which links to that URL like this:
<g:link controller="HelloWorld">Go!</g:link>
I noticed a strange bug where if my controller name prefix is only one "word" such as HelloController, then:
<g:link controller="Hello">Go!</g:link>
... will always generate URLs which point to:
/hello/index
... instead of:
/hello
I refactored and renamed it to other single word controllers and the problem persisted. I was using Grails 2.2.2 so I upgraded to Grails 2.3.4 and was surprised this bug still existed. Renaming the controller to any two word prefix, like HomePageController, HelloWorldController, OneTwoController, etc, is a workaround for now.
This is my first time really using GSP. Am I doing something wrong?
URLs are part of the user experience and should be clean, so the index problem is really annoying.
It's inconvenient, but you can eliminate the index part of the URL for single word controllers by mapping each page and omitting the action.
grails-app/config/UrlMappings.groovy
class UrlMappings {
static mappings = {
"/hello" (controller: "hello")
"/settings" (controller: "settings")
"/dashboard" (controller: "dashboard")
...

What is the difference between Redirect and RedirectToAction in ASP.NET MVC?

What is the difference between Redirect and RedirectToAction other than their return type?
When do we use each? Explanation with any real life scenario would help me greatly.
I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling id parameter and returning proper view.
RedirectToAction lets you construct a redirect url to a specific action/controller in your application, that is, it'll use the route table to generate the correct URL.
Redirect requires that you provide a full URL to redirect to.
If you have an action Index on controller Home with parameter Id:
You can use RedirectToAction("Index", "Home", new { id = 5 }) which will generate the URL for you based on your route table.
You can use Redirect but must construct the URL yourself, so you pass Redirect("/Home/Index/5") or however your route table works.
You can't redirect to google.com (an external URL) using RedirectToAction, you must use Redirect.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table.
Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
Best Practices: Use RedirectToAction for anything dealing with your application actions/controllers. If you use Redirect and provide the URL, you'll need to modify those URLs explicitly when your route table changes.

Url Mapping, Url redirection / rewriting

I want to redirect my '/' uri to a given controller's action. I used to do it via the following code in UrlMappings.groovy:
"/"(action: "highlights", controller: "project")
However, this does not rewrite the url in the browser. It redirects to the correct controller's action, but the browser's navigation Url is still at myProject/. I would like it to be updated to the "correct" uri so that reloads, etc. use the "correct" uri.
I tried:
"/"(uri: "/project/highlights")
but I get a status 404 response.
Any suggestions on how I should proceed?
UrlMappings is only for how to map the url to your controller.action. It won't do any redirect.
If you want to do url redirect, you probably need to setup a http server like apache in front of your application server or the easier way is to just setup a controller.action to do the redirect manually.
"/"(action: "redirect", controller: "project")
In the project controller, and redirect action, just redirect the user to your highlights action.
Your application is deployed to myProject/ context. Hence root ("/") for your application is myProject/. What you want to do is to deploy your application to your server root context. You can do this by setting grails.app.context = “/” in your Config.groovy.

Grails URL mapping cause error on GSP

I have a site that have URL similar to this:
/mysite/admin/controller/action/id
/mysite/search/controller/action/id
/mysite/user/controller/action/id
I have my URL mapping like this
"/$prefix/$controller/$action?/$id?"{
constraints {}
}
I am able to get to the controller correctly.
But on the GSP side
<g:link controller="controller">abc</g:link> ==> abc
Notice how I lose the prefix between mysite and the controller.
You can use named url mappings and then pass the prefix as part of the params:
URLMappings:
name prefix: "/$prefix/$controller/$action?/$id?"{
constraints {}
}
GSP:
<g:link mapping="prefix" params="[prefix:$prefix, controller:...]">abc</g:link>
To use sortableColumn, just put all of the URLMapping parameters in the params property:
<g:sortableColumn property="col" title="title" params="[ prefix: 'prefix', controller:'controller', action:'action']" />
It works when you hit the URL in browser, because prefix is available in URL. It does not work when you use link tag to create url, because grails does not have information about which prefix should be used for this controller. You will need to provide the value for prefix to link tag.
Try this
<g:link controller="controller" params="[prefix:'admin']">abc</g:link>
in-short - You have to pass those dynamic variables as params if you want link re-writing to consider them. Read more docs here

Resources