Posts Tagged ‘Groovy on Grails’

Acegi Security Grails Acegi Plugin Acegi Tutorial

Number of View :3862

Tags:

No Comments


Introduction to Grails plugins Grails Plugins Tutorials Understanding Grails Plugins

What is Grails Plugins

What is a plugin

One of the most important factors which makes Grails very configurable is the concept of plug and play plugins. Almost any functionality which needs to be coded in a web application can be introduced via plugins without any code to be written. The beauty of plugin lies in, we donot have to write any code but just include the plugin and customize codes to our requirement.

A plugin is nothing but a regular Grails application with this special file; but when plugin is installed, the plugin structure slightly differs.

Plugin’s structure

When a plugin is installed into a project, the contents of grails-app directory goes into a directory like $PROJECT_HOME/plugins/example-1.0/grails-app . They will NOT be copied into the main source tree. A plugin doesnot tamper a project’s primary source tree.

The plugin directory structure appears as below :

+ grails-app
     + controllers
     + domain
     + taglib
     etc.
 + lib
 + src
     + java
     + groovy
 + web-app
     + js
     + css

The static resources such as those inside the web-app directory will be copied into the project’s web-app directory under a special “plugins” directory for example web-app/plugins/example-1.0/js . It is therefore the responsibility of the plugin to make sure that it references static resources from the correct place. For example if you were

<g:createLinkTo dir="/plugins/example/js" file="mycode.js" />

To make this easier there is a special pluginContextPath variable available that changes whether you’re executing the plugin standalone or whether you’ve installed it into an application:

<g:createLinkTo dir="${pluginContextPath}" file="js/mycode.js" />

At runtime this will either evaluate to /js or /plugins/example/js depending on whether the plugin is running standalone or has been installed in an application.

Java & Groovy code that the plugin provides within the lib and src/java and src/groovy directories will be compiled into the main project’s web-app/WEB-INF/classes directory so that they are made available at runtime.

What a Plugin can and can’t do

A plugin can do just about anything, but a good plugin should be as intrusive as possible. It should use convention over configuration where possible, otherwise provide an artifact in a project’s grails-app/conf directory where configuration can be done.

One thing a plugin cannot do though is modify the web-app/WEB-INF/web.xml or web-app/WEB-INF/applicationContext.xml files. A plugin can participate in web.xml generation, but not modify the file or provide a replacement. A plugin can NEVER change the applicationContext.xml file, but can provide runtime bean definitions .

Number of View :3663

Tags:

No Comments


Groovy Grails Quick Start Groovy Getting Started Grails Define Groovy

Quick Tutorial to run Groovy On Grails:

The following makes it simple to start a grails project with step wise demonstration.

Quick Start Steps :

  1. Create a Grails project
  2. Configure a Data Source (Optional)
  3. Create a Domain Class
  4. Create a controller
  5. Create Views(Optional)
  6. Start Grails

Step : 1 Create a Grails project

Once you have installed Grails you can use the built-in target for creating new projects:

grails create-app project-name

The project structure below:

%PROJECT_HOME%
    + grails-app
       + conf                 ---> location of configuration artifacts
           + hibernate              ---> optional hibernate config
           + spring                 ---> optional spring config
       + controllers          ---> location of controller artifacts
       + domain               ---> location of domain classes
       + i18n                 ---> location of message bundles for i18n
       + services             ---> location of services
       + taglib               ---> location of tag libraries
       + util                 ---> location of special utility classes
       + views                ---> location of views
           + layouts              ---> location of layouts
   + lib
   + scripts                  ---> scripts
   + src
       + groovy               ---> optional; location for Groovy source files
                                   (of types other than those in grails-app/*)
       + java                 ---> optional; location for Java source files
   + test                     ---> generated test classes
   + web-app
       + WEB-INF

Step : 2 Configure a Data Source (Optional)

The “create-app” command creates a DataSource.groovy file with default contents .

Each data source is configured with an in-memory HSQLDB database ,which is the default configuration.The code looks like :

dataSource {
  pooled = false
  driverClassName = "org.hsqldb.jdbcDriver"
  username = "sa"
  password = ""
}
// environment specific settings
environments {
  development {
    dataSource {
      dbCreate = "create-drop" // one of 'create', 'create-drop','update'
      url = "jdbc:hsqldb:mem:devDB"
//    loggingSql = true
    }
  }
  test {
    dataSource {
      dbCreate = "update"
      url = "jdbc:hsqldb:mem:testDb"
    }
  }
  production {
    dataSource {
      dbCreate = "update"
      url = "jdbc:hsqldb:file:prodDb;shutdown=true"
    }
  }
}

Configuring the data source is a simple matter of changing the values for the desired database and driver and placing the driver jar file in the <..>/lib directory. Properties set in the dataSource node are inherited by the children.

Step : 3 Create a Domain Class

AT first please go to the project directory.

cd my-project
grails create-domain-class ClassName

A domain class is a persistent object and all properties are by default persisted to the database

class ClassName {
    String Course

}

Step : 4 Create a controller

Controllers are request dispatchers in Grails applications they handle web requests and URLs of the request map to a controller class and a closure within the class.

grails create-controller ClassName

Class will be created by name ClassNameController.groovy in path GRAILS_HOME/grails-app/controllers/my/project. You can edit it with your favorite text editor or IDE

Open up this controller and change it as follows to use dynamic Scaffolding which dynamically generates your application at runtime:

class ClassNameController {
     def scaffold = ClassName
}

Alternatively, you could also have run “grails generate-all“, which creates all the scaffolding for you, and left the generated controller alone, instead of replacing it with the default scaffolding. It might be worth learning from.

Step : 5 Start Grails

To start your Grails app run the following target

grails run-app

This will startup an instance of the Jetty servlet engine running on port 8080. To access the list of domain class records open up a browser and type:

http://localhost:8080/project-name/ClassName/list

Or, as the “list” closure is the default action for the ClassNameController you can type:

http://localhost:8080/project-name/ClassName
          Note:

To increase response times during developing try increasing your maximum heap size by setting the JAVA_OPTS environment variable to something like: ‘-Xmx512m’ – this will set the maximum heap size to 512Mb when running Grails hence resulting in  improvement in response.

Number of View :3827

Tags:

No Comments


Basics Of Groovy Define Groovy Introduction to Groovy

Basics on Groovy Tutorials

Groovy is a scripting language for Java,thus enabling java based development faster than ever before.It runs on grails platform.Talking particularly about web development, groovy on grails cuts more than half of the development work as it creates all the controllers and views itself from the domain classes(model). So,in the nut shell when it comes to MVC pattern in java based web development,groovy is the cherry on the cake,as it cuts down more than half of the development work and it recoganizes all the java pre-existing APIs,thats correct we can use java code in groovy as good as groovy itself.

Following steps describe how to install a binary distribution of Groovy.

  1. Atfirst, Download a binary distribution of Groovy and unpack it into some file on your local file systemset your GROOVY_HOME environment variable to the directory you unpacked the distribution add GROOVY_HOME/bin to your PATH environment variable
  2. Set your JAVA_HOME environment variable to point to your JDK. On OS X this is /Library/Java/Home, on other unixes its often /usr/java etc. If you’ve already installed tools like Ant or Maven you’ve probably already done this step.
  3. You should now have Groovy installed properly. You can test this by typing the following in a command shell:

groovysh
Which should create an interactive groovy shell where you can type Groovy statements. Or to run the Swing interactive console type:

groovyConsole
To run a specific Groovy script type:

Following steps describe how to install grails.

  1. Download latest version of Grails.
  2. Set the environment variable GRAIL_HOME to the home folder of Grails/bin,where you have dwnloaded it.
  3. Set PATH env to GRAIL_HOME/bin

Installation can be checked with grails command line options.

Commands like

  1. grails create-app project-name(Creates a new project)
  2. grails create-domain-class(creates domain class from current working directory as the project folder)
  3. grails generate-views(creates views from current working directory as the project folder)
  4. grails run-app (Runs project)

Grails is an open source framework.It is very flexible and configurable.It lessens the burden of developer as it creates almost all basic codes for domain classes,controllers and views.It is a nice replacement to struts or JSF where all these need to be written from scratch.

Number of View :3791

Tags:

No Comments



SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache