Use controllers globally in Grails - grails

I'm new to Grails and am trying to build a CMS with it.
I want the navigation menu to read from the database so a new page will automatically get a link in the navigation. I've been reading Grails: use controller from index.gsp and related questions, but the answers don't seem to work for me. :(
I've created a domain class named Navigation and a template called _header.
In the "Navigation/list" namespace everything works fine, but outside I can't get to the Navigation data.
I've setup url mapping like so:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(controller : "Navigation", action : "list")
"/"(view:"/index")
"500"(view:'/error')
}
}
But that doesn't seem to work. Any clues on what could be the problem?

You have two mappings for "/", your new one and the original one: "/"(view:"/index") - for starters you'll need to remove the other one.

Not sure if you are aware of this, but there is an open source CMS built in Grails called Weceem. If you need to use it as part of another Grails application, there is also a grails plug in for Weceem, so you can use it as part of your app.
It might worth looking into it before building a complete new CMS :-)

I was looking at the problem all wrong, urlmapping only made the index.gsp redirect to navigation/list. What I was looking for was the
DomainClass.findAll( String query )
propertie to use in the g:each tag
<g:each in="${Navigation.findAll('from Navigation as n where n.css=?', ['ctBoven'])}" var="oNavigation" status="i">
This allows me to read any database from any page.

Related

Grails Controller and URL Mapping Magic

Grails 3.0.1 here. I'm looking to accomplish a specific URL/controller structure. My app deploys at the root context (/), meaning locally it runs as http://localhost:8080, and non-locally as http://someserver.example.org.
I want everything under /app/* to be authenticated and considered to be part of the "core app" (requires login). Anything outside of that URL is considered to be a part of the public website (unauthenticated). However, I want /app/ itself to just be a placeholder of sorts; I do not want it to be a Grails controller. Hence:
http://localhost:8080/app may be configured (UrlMappings?) to bring up a login page
http://localhost:8080/app/<controller>/<action> follows typical Grails controller/action suit, and would invoke the correct controller action
Hence http://localhost:8080/app/order/create would be authenticated and, if logged in, invoke the OrderController#create action, which might render a createOrder.gsp.
I'm wondering what the Grails 3.x approach is to:
Allowing /app/ to exist, but not as a controller (like I said, perhaps just redirecting/mapping to a login page)
Allowing anything underneath /app/ to follow the controller/action paradigm
Thoughts on how to implement this?
Update
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/app/$controller/$action?/$id?" {
???
}
"/"(view:"/index")
"500"(view:'/error')
"404"(view:'/notFound')
}
}
Grails has the possibility to declare namespaces for Controllers. With this, you can put all your controllers under the namespace 'app', which should result in exactly your second question. See docs for more details.
The security restriction then be accomplished with normal spring security settings (#Secured e.g).

Grails URL mapping working for controller but not view

Somehow I have gotten sideways with my Grails MVC mapping and I do not understand how.
I have a controller, AController, which I generated using the Grails command line wizard. At a later date I generated a view for that controller to customize the view.
AController is in [project]/grails-app/controllers/[package]/AController.groovy and the view .gsp's are in [project]/grails-app/views/A/.
The URLMappings.groovy has:
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
When I run the application and enter a url of the form: localhost:8080/[project]/A/list I reach, as expected, the method A.list in AController.groovy.
However, when I then return from A.list expecting the framework to route to list.gsp in [project]grails-app/views/A/ I see a 500 error with the message:
"URL mapping must either provide a controller or view name to map to!"
Obviously I am doing something stupid but I cannot see what it was that I broke. URLMappings.groovy looks correct. File locations look correct. Scaffolding seems properly customized.
Any suggestions?
Du-Oh
The problem was that there was no .count for an array. For some reason my brain insists on .count and not .size(). Stupid human error.

Grails set homepage and disable back button

I have two simple question i couldnt managed to find the answer in the internet. There are:
a) how can i set grails mainpath to, for example, controller "cnt" action "action", instead of index.gsp (im using netbeans)
b) how can i not allow page backward in my website?
a) how can i set grails mainpath to, for example, controller "cnt" action "action", instead of index.gsp (im using netbeans)
If you want to change the start page you can do this in UrlMappings.groovy
class UrlMappings {
static mappings = {
"/"(controller:"cnt", action:"action")
}
}
b) how can i not allow page backward in my website?
impossible

How to remove controller from grails programmatically?

Im having some conflicts with the Searchable plugin.
I have a filter which fetches the controller in question in the before phase. It fetches the requested controller by finding the first controller that has the same name as controllerName which is a property available in grails filters.
however, my problem is that the Searchable plugin has its own SearchableController, and i myself have made my own version of the SearchableController. The effect of this is that i have two classes with the same name that would match controllerName in my find logic.
controller = grailsApplication.controllerClasses.find { c ->
c.logicalPropertyName == controllerName
}
Can i somehow remove the SearchableController that comes with the plugin? What are my options?
Maybe this could help you:
http://grails.org/Searchable+Plugin+-+SearchableController+and+view
I found an existing post on this topic:
Disable grails Searchable plugin default search page?
I worked around the issue by renaming my SearchableController class to SearchController. Don't like workarounds tho...

Grails UrlMappings with .html

I'm developing a Grails web application (mainly as a learning exercise). I have previously written some standard Grails apps, but in this case I wanted to try creating a controller that would intercept all requests (including static html) of the form:
test 1
test 2
test 3
test 4
The intent is to do some simple business logic (auditing) each time a user clicks a link. I know I could do this using a Filter (or a range of other methods), however I thought this should work too and wanted to do this using a Grails framework.
I set up the Grail UrlMappings.groovy file to map all URLs of that form (/$myPathParam?) to a single controller:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
}
}
"/$path?" (controller: 'auditRecord', action: 'showPage')
"500"(view:'/error')
}
}
In that controller (in the appropriate "showPage" action) I've been printing out the path information, for example:
def showPage = {
println "params.path = " + params.path
...
render(view: resultingView)
}
The results of the println in the showPage action for each of my four links are
testJsp.jsp
testGsp.gsp
testHtm.htm
testHtml
Why is the last one "testHtml", not "testHtml.html"?
In a previous (Stack Overflow query) Olexandr encountered this issue and was advised to simply concatenate the value of request.format - which, indeed, does return "html". However request.format also returns "html" for all four links.
I'm interested in gaining an understanding of what Grails is doing and why. Is there some way to configure Grails so the params.path variable in the controller shows "testHtml.html" rather than stripping off the "html" extension? It doesn't seem to remove the extension for any other file type (including .htm). Is there a good reason it's doing this? I know that it is a bit unusual to use a controller for static html, but still would like to understand what's going on.
This is related to content negotiation, which you can read about in section 6.8 of the Grails user guide. If Grails recognises the extension as a particular type, the extension is removed from the URL and the type is added to the "format" parameter.
You can disable this behaviour by adding this entry to grails-app/conf/Config.groovy:
grails.mime.file.extensions = false

Resources