Back Home

View

  • Official Web Layer documentation
  • Grails also supports converting haml templates files ( haml) to gsp, reducing much of the html bloat

Pass gsp data to javascript:

var ary = ${fields.encodeAsJavaScript()};

Returning json or other static values

def result = "json or static string" 
render result 

Client Side Validation

Grails can use the jquery client side validation:

compile ":jquery-validation-ui:1.4.9"

GORM

JDBCTemplate in Grails

Adding Spring's JDBCTemplate support to your grails appliation is really quite painless. This covers adding JDBCTemplate to a test class but this can be extended to any area of the application Edit the file conf\spring\resources.groovy so that it contains the following:

import org.springframework.jdbc.core.JdbcTemplate
import org.apache.commons.dbcp.BasicDataSource
// Place your Spring DSL code here

beans = {
// uses the grails dataSource from DataSource.groovy
jdbcTemplate(JdbcTemplate) {
   dataSource = ref('dataSource')
}

// use a different datasource
otherDataSource(BasicDataSource) {
   driverClassName = "com.mysql.jdbc.Driver"
   url = "jdbc:mysql://localhost:3306/otherDatabase"
   username = "uname"
   password = "pass"
}

otherJdbcTemplate(JdbcTemplate) {
   dataSource = otherDataSource
}
}

Then allow dependency injection to do its work by putting the following at the top of the test class:

class someTests extends GroovyTestCase {
   def jdbcTemplate
   def otherJdbcTemplate
...
}

and finally create a test something like this to exercise the template: void testJdbcTemplate() {

def myResult = jdbcTemplate.queryForList("select * from table1")
def myOtherResult = otherJdbcTemplate.queryForList("select * from table2")
println "myResult size : ${myResult.size()}"
println "myOtherResult size : ${myOtherResult.size()}"
}
<code>
=== Grails Elastic Search ===
* plugin can uses the query dsl [[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html | ES Query Dsl]]
== Grails and Ajax == 
* Introduction [[http://javadeveloper.asia/grails-ajax-tutorial-remotelink-tag/ | grails ajax]] tutorial 

It can be broken down into the following taglibs 

* remoteLink – create an AJAX based link
* remoteFunction – create a javascript code that fires an AJAX request
* remoteField – creates a text control field that fires an AJAX request when the value changes
* formRemote and submitToRemote – tags to make a form serialize it’s values and submit using an AJAX request.

http://grails.org/doc/2.3.x/guide/theWebLayer.html#remoteFormSubmission

== Grails Plugins == 


<code>
grails create-plugin simple

This will create a plugin project which can then be packaged with package-plugin:

grails package-plugin

and installed into Grails applications with install-plugin:

grails install-plugin ../grails-simple-0.1.zip

More details on creating plugins http://grails.org/doc/latest/guide/single.html#plugins

Grails and Hsqldb

  • add runtime 'hsqldb:hsqldb:1.8.0.10' in BuildConfig.groovy
  • set driverClassName = “org.hsqldb.jdbcDriver” in Database.groovy
  • Specify connection url according to your requirements, e.g.: “jdbc:hsqldb:mem:devDB” - to use in-memory DB,
  • or “jdbc:hsqldb:file:prodDb;shutdown=true” - to use file based server

Adding Font Awesomeness

There is a plugin however to add manually is pretty simple (with 2 tricks)

  • Extract the css and font files to the webapp/css and webapp/fonts folders
  • edit the css to remove the ?v4.1.0 version info
  • update the resouces in config.groovy
    grails.resources.adhoc.includes = ['/images/**', '/css/**', '/js/**', '/plugins/**', '/fonts/**']

Config and Services

It seems by default the grailsApplication.config is not available on service construct. @PostConstruct doesn't help however InitializingBean does work:

class QueueJmxService implements InitializingBean {
    def grailsApplication
    def j4pClient;
    def urlStr


    public void afterPropertiesSet() throws Exception {
        println "post construct " +  grailsApplication.config
    }

Changing the default war name

Edit your grails-app/conf/BuildConfig.groovy and add this line:

grails.war.destFile = 'cylo.war'

Misc

wget -c --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" http://download.oracle.com/otn-pub/java/jdk/7u9-b05/jdk-7u9-linux-i586.tar.gz

Copy the downloaded file to : /usr/java And extract using tar -xvf jdk-7u9-linux-i586.tar.gz

Setup alternative mappings for java

sudo  /usr/sbin/update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.7.0_09/bin/java" 40
sudo  /usr/sbin/update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk1.7.0_09/bin/javac" 40

then set the alternatives

sudo  /usr/sbin/update-alternatives --config java
sudo  /usr/sbin/update-alternatives --config javac

Stopping grails

 .kill-run-app

Docker Grails

docker run -i -t -p 8080:8080 --rm -v $PWD:/app onesysadmin/grails grails
 
grails.txt · Last modified: 2015/03/05 03:58 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd