How do you change the default homepage in a Grails application? - grails

What is the configuration setting for modifying the default homepage in a Grails application to no longer be appName/index.gsp? Of course you can set that page to be a redirect but there must be a better way.

Add this in UrlMappings.groovy
"/" {
controller = "yourController"
action = "yourAction"
}
By configuring the URLMappings this way, the home-page of the app will be yourWebApp/yourController/yourAction.
(cut/pasted from IntelliGrape Blog)

You may try as follows
in the UrlMappings.groovy class which is inside the configuration folder:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
//"/"(view:"/index")
"/" ( controller:'Item', action:'index' ) // Here i have changed the desired action to show the desired page while running the application
"500"(view:'/error')
}
}
hope this helps,
Rubel

Edit UrlMappings.groovy
Add for example add this rule, to handle the root with a HomeController.
"/"(controller:'home')

Use controller, view and action parameter by the following syntax:
class UrlMappings {
static mappings = {
"/" (controller:'dashboard', view: 'index', action: 'index')
"500"(view:'/error')
}
}

Simple and Neat
Go to File: grails-app/conf/UrlMappings.groovy.
Replace the line : "/"(view:"/index") with
"/"(controller:'home', action:"/index").
Home is Your Controller to Run(Like in spring security you can use 'login' ) and action is the grails view page associated with your controller(In Spring Security '/auth').
Add redirection of pages as per your application needs.

All the answers are correct!
But let's imagine a scenario:
I mapped path "/" with the controller: "Home" and action: "index", so when i access "/app-name/" the controller Home gets executed, but if i type the path "/app-name/home/index", it will still be executed! so there are 2 paths for one resources. it would work until some one finds out "home/index" path.
another thing is if I have a form without any action attribute specified, so by default it will be POST to the same controller and action! so if the form is mapped to "/" path and no action attribute is specified then it will be submitted to the same controller, but this time the path will be "home/index" in your address-bar, not "/", because it's being submitted to the controller/action not to the URI.
To solve this problem what you have to do, is to remove or comment out these lines.
// "/$controller/$action?/$id?(.$format)?"{
// constraints {
// // apply constraints here
// }
// }
So now when you access "/", will work. but "home/index" will not. But there's a one flaw, now you have to map all the paths to the controllers manually by explicitly writing into URLMapping file. I guess this would help!

If anyone is looking for the answer for gails 3.x, they moved UrlMappings.groovy to grails-app/controllers/appname
As the below answers say, just ed it the line starting with "/".
In my case its:
"/"(controller:"dashboard", view:"/index")

Related

Grails <g:link> rewriting links to match UrlMappings instead of controller/action

Currently using grails 4.0.3.
I'm trying to generate a link to a controller action using a simple <g:link> tag:
<g:link controller="myController" action="myAction" params="[someParam:'myValue']">Link text</g:link>
In my UrlMappings file, I have this controller mapped to a common URL for external calls. This mapping forces some parameters that I want forced when coming to this mapping:
class UrlMappings {
static mappings = {
"/service/someExposedUrl" {
controller = "myController"
action = "myAction"
someParam = "defaultValue"
}
}
}
However, the link that gets written to the page in my application gets written using the UrlMapping definition.
http://serverUrl/service/someExposedUrl?someParam=<ignored>
instead of
http://serverUrl/myController/myAction?someParam=myValue
In this case, the parameters are ignored on the /service URL because they're hard-coded in the UrlMapping.
Is there a way to force grails to link to the specified controller/action instead of the mapping?
Up front caveat: I have not tested this for this specific situation, though I have used these pieces individually. Second caveat: I would not be remotely surprised to learn there's an even better way to do this...grails has a lot of options sometimes!
You should be able to create a named mapping in your UrlMappings, then reference that in your createLink.
UrlMappings:
name arbitraryName: "/myController/myAction" {
controller = "myController"
action = "myAction"
}
Link:
<g:link mapping="arbitraryName" params="[someParam:'myValue']">Link text</g:link>
You may be able to get what you need with a combination of Custom URL mapping and the exclude keyword in the original URLMappings.
Create MyControllerUrlMappings. This guide gives several examples of different ways of writing them out. I found it helpful.
class MyControllerUrlMappings {
static mappings = {
// Url Mappings specific only to this controller
}
}
https://guides.grails.org/grails_url_mappings/guide/index.html#_multiple_urlmappings_files
Your app will still be running the original UrlMappings file though. So you'll have the new custom Url mappings and the default ones. To fix that you can exclude your controller and it's methods in the original UrlMappings file.
class UrlMappings {
static excludes = [
"/myController",
"/myController/*"
]
static mappings = {...}
...
Not as elegant as I'd like, but in a similar situation this is how I got it working.

Grails: URL mapping with .gsp extension/format

I have a url request like www.xyz.com/customer/list.gsp
When I try to map the url to remove .gsp:
"/customer/list.gsp"(controller: "customer") {
action = "list"
}
grails application won't recognize the url and is throwing 404 error. Am I missing something here?
If you want to remove .gsp from the url then you can use a mapping like this...
"/customer/list"(controller: "customer") {
action = "list"
}
You could also do this...
"/customer/list"(controller: "customer", action: "list")
If you want 1 mapping for all the actions in the controller, you could do this:
"/customer/$action"(controller: "customer")
The default generated mapping includes "/$controller/$action" which allows you map to any action in any controller.
With any of that, sending a request to /customer/list would work.
Update: apparently it is fine to map to GSPs. I still think that the info below may be helpful so I'm leaving the answer up, but perhaps I have misunderstood your question.
Original response:
You shouldn't be mapping to or requesting gsps at all. They're used to generate views, but are not viewable without rendering.
Instead, go to url like www.xyz.com/customer/list and map that like
"/customer/list" (controller: "customer") {
action = "list"
}
Or even better, you don't need a custom mapping for each endpoint. A default like this will work:
"/$controller/$action?/$id?" { }
Your CustomerController will render the list.gsp in the list action.

different mapping for the same url

I would like to use a different mapping for the same url http://localhost:8080/myapp/ when the user is logged in (session.user)
Actually, as default Im giving the path when the url is "/" to AppController and 'index' action... but if I try to redirect inside the index action when the user is logged to my UserController (also index action), the path changes to http://localhost:8080/myapp/user/index . Thats not what I`m looking for.
There is a plenty of website (twitter, facebook..) that apply this method, but couldn`t understand how can it be done in Grails, without using the same action for example (AppControlle>index) and render different views when user is active.
static mappings = {
"/"(controller:"app",action:"index")
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"500"(view:'/error')
"404"(view:'/notFound')
}
About your mention about twitter, facebook... I think it's possible that they use different mapping based on the request is POST or GET. In Grails, we can do that kind of mapping like this:
name home: "/" {
controller = [GET: "app", POST: "user"]
action = [GET: "index", POST: "userIndex"]
}

problem with routing asp.net

I am fairly new to asp.net and I have a starting URL of http://localhost:61431/WebSuds/Suds/Welcome and routing code
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{page}", // URL with parameters
new { controller = "Suds", action = "Welcome", page = 1 } // Parameter defaults
);
routes.MapRoute(
"Single",
"{controller}/{action}",
new { controller = "Suds", action = "Welcome" }
);
I am receiving the following error: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Can anyone please help me figure out how to route the beginning url to the controller.
Because the first part of your URL is WebSuds, the framework is trying to map the request to WebSudsController instead of SudsController. One thing you can try is to change the url parameter of your route to "WebSuds/{controller}/{action}".
Route tables are searched on a first come first served basis. Once an appropriate match is found any following routes will be ignored.
You need to add "WebSuds/{controller}/{action}" and put it above the default route. The most specific routes should always go above the more generic ones.
With the following Routes and Url you have given make sure you have these Methods in your Controller
public class SudsController
{
public ActionResultWelcome(int? page)
{
return View();
}
}
Since you have created two routes of the same Action, one with a parameter page and another without. So I made the parameter int nullable. If you don't make your parameter in Welcome nullable it will return an error since Welcome is accepting an int and it will always look for a Url looks like this, /WebSuds/Suds/Welcome/1. You can also make your parameter as string to make your parameter nullable.
Then you should have this in your View Folder
Views
Suds
Welcome.aspx
If does doesn't exist, it will return a 404 error since you don't have a corresponding page in your Welcome ActionResult.
If all of this including what BritishDeveloper said. This should help you solve your problem.

How do you change the controller text in an ASP.NET MVC URL?

I was recently asked to modify a small asp.net mvc application such that the controler name in the urls contained dashes. For example, where I created a controller named ContactUs with a View named Index and Sent the urls would be http://example.com/ContactUs and http://example.com/ContactUs/Sent. The person who asked me to make the change wants the urls to be http://example/contact-us and http://example.com/contact-us/sent.
I don't believe that I can change the name of the controller because a '-' would be an illegal character in a class name.
I was looking for an attribute that I could apply to the controller class that would let me specify the string the controller would use int the url, but I haven't found one yet.
How can I accomplish this?
Simply change the URL used in the route itself to point to the existing controller. In your Global.asax:
routes.MapRoute(
"Contact Us",
"contact-us/{action}/",
new { controller = "ContactUs", action = "Default" }
);
I don't believe you can change the display name of a controller. In the beta, the controller was created using route data "controller" with a "Controller" suffix. This may have changed in RC/RTM, but I'm not sure.
If you create a custom route of "contact-us/{action}" and specify a default value: new { controller = "ContactUs" } you should get the result you are after.
You need to configure routing. In your Global.asax, do the following:
public static void RegisterRoutes(RouteCollection routes)
{
...
routes.MapRoute(
"route-name", "contact-us/{action}", // specify a propriate route name...
new { controller = "ContactUs", action = "Index" }
);
...
As noted by Richard Szalay, the sent action does not need to be specified. If the url misses the .../sent part, it will default to the Index action.
Note that the order of the routes matter when you add routes to the RouteCollection. The first matched route will be selected, and the rest will be ignored.
One of the ASP.NET MVC developers covers what Iconic is talking about. This was something I was looking at today in fact over at haacked. It's worth checking out for custom routes in your MVC architecture.
EDIT: Ah I see, you could use custom routes but that's probably not the best solution in this case. Unless there's a way of dealing with the {controller} before mapping it? If that were possible then you could replace all "-" characters.

Resources