I'm new to grails. I'm trying to develop a simple page with just a dropdown that when the user makes a selection an AJAX call is made to the database. Thing is that there is are no domain files at all in my application. I only have controllers and views and I intend to keep it this way. So I basically want to use grails to issue a mySQL Select statement through AJAX and get the results.
You could do the below:
See http://groovy.codehaus.org/Tutorial+6+-+Groovy+SQL for a tutorial on executing SQL using the built-in Groovy SQL library. You can then create a controller action and map your resultset to JSON, and you won't even need a GSP view file. Here's an example Controller class that will do basically what you need.
import groovy.sql.Sql
class MyController {
def sessionFactory
def myAction() {
def sql = new Sql(sessionFactory.currentSession.connection())
sql.execute("select ....") //execute SQL using Groovy SQL
render(contentType:"application/json") {
//render your DB query results as JSON
//you could also use JsonBuilder to render JSON output
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.