i want to make grails mapping template.
the example like this.
URL :
http://localhost:8080/controller/action/id/
i have 2 gsp views
1.Product
2.Product_create
when variable id == "create"
grails automatically display product_create if not grails will display product.
is anyone can solve this??
please correct my english. thankyou.
Something like:
def product( String id ){
if( 'create' == id ) return render( view:'product_create' )
def product = doSomethingWithId()
[ product:product, ... ] // here the default "product.gsp" will be rendered
}
Related
We're migrating from Grails 2.x to 3.x. I can observe some different behaviour when using the forward function:
class FooController {
def index() {
forward controller: 'foo', action : 'bar', params: params
}
def bar() {
render(
view: "/foo/foo"
)
}
}
When calling http://localhost:8080/foo?test=1 and halting in the bar() method I can see that params looks like that:
params = {GrailsParameterMap#11597} size = 4
0 = {LinkedHashMap$Entry#11607} "test" ->
key = "test"
value = {String[2]#11612}
0 = "1"
1 = "1"
1 = {LinkedHashMap$Entry#11608} "controller" -> "foo"
2 = {LinkedHashMap$Entry#11609} "format" -> "null"
3 = {LinkedHashMap$Entry#11610} "action" -> "bar"
As you can see the value of test is saved twice as a String[]. This behaviour is different than it used to be in Grails 2.5.6.
Is there any way to set a flag to the Grails forward function in order to not get the params passed to the redirect controller?
I think you don't need to add the param. forward automatically forwards your parameters. It's optional. If you add it, it will duplicate the values. Try with only:
forward controller: 'foo', action : 'bar'
I trying to make a dynamic URL mapping which you can pass any query within the URl. So at this moment in time I want to render the key/values of the query but without the controller,method and action information.
So when I pass person?name=Mark&age=60
it renders ['name':'Mark', 'age':'60', 'controller':'Person', 'method':'GET', 'action':'getPerson']
How am I able to get just the query's in the url and not the other information about the controllers etc.
UrlMappings
"/person"{
controller = "Person"
method = 'GET'
action = "getPerson"
}
PersonController
def getPerson(){
render params
}
I've done the following to remove them from the render by doing this:
HashMap search = params
String action = "action"
String controller = "controller"
String method = "method"
search.remove(action)
search.remove(controller)
search.remove(method)
render search
I think the best you can do is to remove the parameters that you know about and do not want to see:
def getPerson(){
render params.findAll { !(it.key in ['action', 'controller', 'method']) }
}
I have the following problem:
My object route contains a list of routepoints. When I alter this list, the changed list is saved. But when the next method accesses the list, it seems like the change reverted. I ruled some kind of transaction rollback out, b/c on the end of the altering method, i acces the list by loading it from the database and it still has the right (altered) size. Here's the code:
First the altering method:
def removeStationFromRoute(station){
def driver = Driver.get(Integer.valueOf(requestAccessService.getParams().userId))
def route = driver.routes.find{
it.routeDate == new Date().clearTime()
}
def rp = Routepoint.findByStation(station)
route.routepoints.remove(rp)
def newRoute = driver.routes.find{ it.routeDate == new Date().clearTime()}
println 'new route size: ' + newRoute.routepoints.size()
def newRoute2 = Route.get(route.id)
println 'new route from db size: ' + newRoute2.routepoints.size()
}
Both prints return a size of 5, which is correct. Right after this method is carried out, this method is executed:
def getDriverRoute(){
def driver = User.get(Long.valueOf(params.userId))
def route = driver.routes.find{ it.routeDate == new Date().clearTime()}
println 'serialized route size: ' + route.routepoints.size()
def routeString = jobService.serializeRoute(route)
log.info('Route with ' + route.routepoints.size() + " stations serialized for User " + driver.encodeAsHTML())
render routeString
}
Which prints a size of 6, as if no change happened to the list. I already tried saving the driver, the route and the routepoint after the change is made in the "removeStationFromRoute"-List, as well as checking the three objects for errors. Didn't help.
Thanks for any ideas what to do!
I guess you have a 1-N relationship between Route and Routepoints? Something like
class Route {
static hasMany = [routepoints: Routepoint]
}
class Routepoint {
static belongsTo = [route: Route]
}
You should not add/remove routpoints using the add/remove methods of the Collection interface. Instead you should use the addTo*/removeFrom* GORM methods, e.g.
route.addToRoutepoints(routepoint)
route.removeFromRoutepoints(routepoint)
Firstly,
After you have used route.removeFromRoutepoints(routepoint)
to remove the mapping of the Routepoint with Route in the first method, the Route Object Still needs to be persisted using .save/.merge method.(Check here )
Secondly.
In hibernate, using Domain.get(id) will not always hit the Database, IF the object already cached in the Hibernate session. Check here
Hope it helps...
I would like to get the field names of a class and maybe store it in a list. Can anyone help? Thanks.
You can try this to get field names of domain class.
YourClass.declaredFields.each {
if (!it.synthetic) {
println it.name
}
}
You can use gormPersistentEntity for any domain object, this works with Grails 2.4.4 at least:
def names = Person.gormPersistentEntity.persistentPropertyNames
//returns ['firstName', 'lastName'...]
you can also get natural name using GrailsNameUtils like so:
def naturalNames = Person.gormPersistentEntity.persistentPropertyNames.collect {
grails.util.GrailsNameUtils.getNaturalName(it)
}
//returns ['First Name', 'Last Name'...]
def capitilizedNames = Person.gormPersistentEntity.persistentProperties.collect{
it.capitilizedName
}
//returns ['FirstName', 'LastName'...]
Just found it out, this one works:
def names = grailsApplication.getDomainClass('com.foo.Person').persistentProperties.collect { it.name }
You can iterate over the fields of a class like this.
YourClass.fields.each { println it.name }
If you need to put them into a list you could use collect() or populate it within the each.
http://groovy.codehaus.org/JN3535-Reflection
I've created a tmpl gsp tag containing a bit of markup that's used throughout the forms in my webapp (/shared/formRow.gsp). I'd like to reference this tmpl gsp tag in a groovy taglib I've created. Is this possible?
Here's the def from my taglib...
def checkboxRow = { attrs ->
def name = attrs.name
def value = attrs.value
def label = attrs.label
def defaultLabel = attrs.defaultLabel
out << "<tmpl:/shared/formRow name='${name}' label='${label}' defaultLabel='${defaultLabel}'>"
out << " ${checkBox(id: name, name: name, value: value)}"
out << "</tmpl:/shared/formRow>"
}
I realise the syntax is a bit different in taglibs (e.g. you need to do ${checkBox(...)} rather than ), but is it possible to reference your own tmpl gsp tag in a similar way? If so, what syntax would I use?
Well, it turns out that it's in the Grails documentation, here.
You should just call the render template method like this:
def formatBook = { attrs, body ->
out << render(template: "bookTemplate", model: [book: attrs.book])
}
Simple really!