In my UrlMapping.groovy I am trying to reach a specific view of controller sending a param, but this does not work:
"/"(view:'/myController/myAction?myParam=value')
"/"(controller:"myController" action="myAction" params:[myParam:"value"])
"/"(controller:"myController" action="myAction" myParam:"value")
Any idea?
Thanks
"/"(controller:"myController", action:"myAction"){ param="value"}
The answer posted here worked for me:
https://stackoverflow.com/a/28870770/695318
It even works for multiple parameters:
"/some/path"( controller:"admin", action:"whatever") {
myparam = "value"
anotherParam = "Hello World"
}
Access from a controller, e.g. AdminController
def whatever() {
println params.myparam
println params.anotherParam
}
value
Hello World
Related
I have successfully set up my Grails application to authenticate the user.
I map controller method arguments using URL params, in my UrlMappings.groovy:
"/$source/owner/$ownerId/show"(controller:"myController", action: "show", method: "GET")
How do I get the values of $source and $ownerId in my #Secured closure?
My controller method looks like this:
#Secured(closure = {
//null
def testSource = params.source
//also null
def testOwnerId = params.ownerId
//null
def owner = request.ownerId
//null
def owner2 = request.getParameter('ownerId')
//contains only query string params
def grailsParams = request['org.codehaus.groovy.grails.WEB_REQUEST']?.params
return true
})
def show(String source, String ownerId) {
...
}
How do I get these values? What am I doing wrong, here?
I thought that this post would provide a solution, but the answer given there didn't work for me:
Is it possible to hack the spring-security-core plugin #Secured annotation to be able to reference request params given to a controller?
I am using the following grails and plugin versions:
grails 2.5.1
compile ":spring-security-core:2.0-RC5"
compile ":spring-security-rest:1.5.3", {
excludes 'com.fasterxml.jackson.core:jackson-databind:'
}
Brief :
Use request.getParameter
Details :
In 2.0 you can use a closure as the annotation's check; there's a brief writeup and example in the docs: https://grails-plugins.github.io/grails-spring-security-core/v2/guide/newInV2.html
You'd express your example as this:
#Secured(closure={
request.getParameter('ownerId') >=123 //this is example
})
Return true to allow access, false to deny access.
Basically I am using the code below for debugging. It renders null and I cannot figure out the
reason. And by the way the else statement will always be executed at this point. At first I
didnt have the save method but I then thought it might fix my issue. May is have to do with
the scope of my domains? As of now I have them set to session scope:
class Info {
static scope = "session"
String name
String smokingStatus
String[] symptom
static constraints = {
}
}
else{//query did not find patient with that id
def patInfo = new Info()
patInfo.name = "Dennis"
patInfo.smokingStatus = "Former Smoker"
patInfo.symptom = ["Cough"]
patInfo.save()
redirect(action:"display", params: [patInfo:patInfo])
//redirect(action:"login")
//return to login action and
}
}
}
def display(){
render params.name
}
Thanks for any help its much appreciated.
You are assign the value of patInfo to variable name patInfo so in the display action you must use:
render params.patInfo
By example if you will use the following:
redirect(action:"display", params: [duck:patInfo])
You must use:
render params.duck
I am redirecting an uploaded image in grails like so:
Controller:
def upload() {
def f = request.getFile('myFile')
if (f == null | f.empty) {
flash.default = "file cannot be empty"
errors.getFieldError("cannot be empty")
return
}
def fName = f.getOriginalFilename()
def picture = new Picture(orgName: fName, urlOrg: "http://localhost/"+fName)
f.transferTo(new File('/Users/sagarmichael/Desktop/photoUpload/'+fName))
println("saved")
redirect(action: 'test', params: [url1:picture.urlOrg] )
}
def test(){
System.out.println(params.url1)
[url1:params.url1]
}
I would expect this to then send url1 to my view called test where I have this:
<img src="${url1}"/>
I would expect this to then show the image on screen.
I have the apache2 config set correctly and when i go to
http://localhost/<imageName>
it works correctly.
What I am getting is this in the url bar on the browser:
http://localhost:8080/FYP/profile/test?url1=http%3A%2F%2Flocalhost%2Flonglogo3.png
any ideas?
If you use the redirect method with params they will appear in the url. To omit this, you will need to think in another way to design your code.
Looking at your code, it appears that you do the upload of some image and need to show the result. I suggest you pass just the name of the image to your other method and mount the link dynamic (not always will be localhost, wright?). The output url will be:
http://localhost:8080/FYP/profile/test?image=longlogo3.png
If you need to omit the name of the image too you will have to store this in the session.
You are mixing model and URL params. If you really need to have it like this, you have to put the content of param 'url1' into the model in the test() action:
def test() {
return [ 'url1': params.url1 ]
}
I hope, you get the point, I definitely recommend to redesign the code ;-)
Try
render(view:"test", model: [url1:picture.urlOrg])
Or
chain(action: "test", model: [url1:picture.urlOrg])
I wrote a plugin to inject a method into all controllers, I wrote this on doWithDynamicMethod {ctx -> }
for (classes in org.codehaus.groovy.grails.commons.ApplicationHolder.application.controllerClasses){
def controllerClass = classes.clazz
controllerClass.metaClass.static.doTestSearch << {args ->
println "this is dynamic insertion -->"+args.toString()
}
classes.class.metaClass.doTestSearch << {args ->
println "this is dynamic insertion -->"+args.toString()
}
}
When i tried to call this method in form in view, by :
<g:form method="POST">
<g:actionSubmit class="save" action="doTestSearch"
value="${message(code: 'default.button.search.label', default: 'Search')}" />
The method doesn't called and returning a 404 error.
But when i tried to call it from controller by :
doTestSearch(params)
It works.
Can someone please explain why is this happening ? And can i call the dynamic method directly from view ?
Thank you in advance
Try this
application.controllerClasses.each {controller ->
controller.metaClass.doTestSearch = {
//Your action code here
}
}
BTW - you can use DynamicController plugin also for adding actions to controllers
It looks like grails can't map the URL for the methods added at runtime but I haven't tested it.
Is it possible to access the current controller instance from within a TagLib? For example:
class FooTagLib {
static namespace = 'foo'
def msg = { attrs, body ->
// Can I get a reference to the current controller here?
}
}
I want to do this because I store some data in a property of the controller and want to access it within the TagLib. I realise this may sound strange, but just humour me....
Inside your msg tagLib:
grailsApplication.getArtefactByLogicalPropertyName('Controller', pageScope.controllerName)
Like Views, you have access to the current controller and action through controllerName and actionName
Try something like this...
def ctl = grailsApplication.getArtefactByLogicalPropertyName('Controller', 'whateverController')