Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual...

33
Grails Mansura Habiba 13 november 2011

Transcript of Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual...

Page 1: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails

Mansura Habiba13 november 2011

Page 2: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Introduction

What is Grails?Runs on the Java Virtual MachineUsed Java like dynamic language Groovy Inspired by Rubi on railsCreated and used by Google

2

Page 3: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Feature of Grails

• Grails is a rapid web application development framework built on Groovy and Spring

• Grails has full stack environement• Grails is modular and extensible through a

plugin system• Just like Spring Grails deployed in any

environment

3

Page 4: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Stack

4

JAVA Virtual Machine

JAVA EE JDK Servlet Apache Common Groovy

SiteMesh

SpringMVC, Beans,

Txns

GORM

Hibernet

HSQLBD

Gan

t (G

roo

vy A

nt)

JUnit

Grails

Controller

View (gsp) Service Domainplugin

Page 5: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Full Stack Web Framework

5

Web MVC GSP Viewes Servlet Container

Database I18n

Test Support Doc Engine

GORM (Data Access)

Build

An easy to use MVC Framework

Embedded ServletSimple but Powerful

Build in DB access and Modelling

In Memory SQL Database

Build in Internationalizartion

WiKi based HTML Doc Engine

Page 6: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails MVC Framework

6

Spring Dispatcher Servlet

Grails Dispatcher Servlet

Simple Grails Controller

Simple Grails Controller Helper

User Interface (GSP)

Grails Controller Grails Service/ Plug-ins

Grails DomainDB

GORM

Template (Sitemesh)

Extends

URI Request

URI Request

View

Model & View

mapping

Page 7: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Comparison of Groovy with Ruby and Java

Consideration Java ( JVM ?)

Ruby on Rails

Groovy and Grails

Reduced Time to Market No Yes Yes

Reduced Costs No Yes Yes

Long term scalability Yes No Yes

Recommended for applications currently on Java and PHP

Yes No Yes

7

Page 8: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Consideration Java ( JVM ?) Ruby on Rails Groovy and Grails

Lines of codes to write (Prototypical form validation example)

20 8 6

Readability Lengthy lines of codes make it less readable

Small size helps in readability Good IDE support and hence increases readability.Also size of code governs readability

Performance High Comparitively lower than Java and Grails

Higher than RoR but lesser than Java

Well-structured and updated knowledge base

Yes No Yes

Adaptability with Agile No Yes YesEnterprise support Yes Small applications.

The support of enterprise technologies is not at the focus of the community

Could be used for prototyping and also for real applications

In built testing tools available No Yes Yes

Developer availability Huge supply Not as much compared to Java

Groovy is a natural transition for Java developers

Scalability Yes No YesRichness of API/libraries available

Yes No Yes

Ability to embed in desktop (Java/C++) applications

Yes No Yes

Ease of deployment No Yes YesLearning curve Yes Depends on the history of the

developer.Generally start with the database schema and generate domain objects, view and controller from there

Depends on the history of the developer.Start with the domain objects and generate view, controller and database schema.A lot more people can write proper domain classes than proper database schemata.

8

Page 9: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Installation (JDK)

• Download JDK from the following Url• Invoke the installer and follow the

installation• Setup JAVA_HOME as User variable• Add % JAVA_HOME %\bin to Path

9

Page 10: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Installation (Grails)

• Download GGTS from the following link http://spring.io/tools/ggts

• Unzip the binary• Plug and play

– No need to set up GRAILS_HOME– No need to modify Path varible– No need to set up Server

10

Page 11: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails Installation Folder

11

IDE

Grails

Server Open Source Licence

Page 12: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Create a Web Application

grails create-app HelloWorld

install-plugin spring-security-

core

s2-quickstart com.grails.tutoria

l User Role

Add properties to generated User

Class

grails generate-all

com.grails.tutorial .User

grails run-app

Application will be available at

http://localhost:8080/HelloWorld

12

Page 13: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails Web Application: Important files

13

Page 14: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails Web Application: Url mapping

• Custome DSL for Mapping URLs to view, controller, action

• Support rich constraints for mechanism• Support HTTP method for REST

14

Page 15: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

15

Grails Web Application: Url mapping

Page 16: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails & validation

16

class User { String login String password String emailInteger ageDate birthDate

static constraints = {login size: 5..15, blank: false, unique: true password size: 5..15, blank: falseemail email: true, blank: false age min: 18birthDate max: new Date()}

}

<g:renderErrors bean="${user}" />

<g:hasErrors bean="${user}"> <ul> <g:eachError var="err" bean="${user}"> <li>${err}</li> </g:eachError> </ul> </g:hasErrors>

<div class="fieldcontain ${hasErrors(bean: userInstance, field: 'firstname', 'error')} "><label for="firstname"><g:message code="user.firstname.label" default="Firstname" />

</label><g:textField name="firstname" value="${userInstance?.firstname}" /></div>

1

2

3

Page 17: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Validating the data : Standerd validation

• nullable - Ensures the property value cannot be null

• blank- Prevents empty fields

• email - Checks for well-formed email addresses

• inList - Displays a combo-box

• Unique - Prevents duplicate values in the database

17

• min, max - Minimum, maximum value for a numeric field

• minSize, maxSize Minimum, maximum length for a text field

• matches - Applies a regular expression against a string value

• validator - Set to a closure or

block to use as a custom validation

Page 18: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails & Internationalization

• Grails ships with several message bundles for a whole range of languages within the grails-app/i18n directory. For example:– messages.properties– messages_da.properties– messages_de.properties– messages_es.properties– messages_fr.properties

18

Default

Page 19: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

19

Grails & Internationalization

Page 20: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

• Reading Messages in the View

• Reading Messages in Controllers

• Reading Messages in Tag Libraries

20

<g:message code="my.localized.content" args="${ ['Juan', 'lunes'] }" />

def show() { def msg = message(code: "my.localized.content", args: ['Juan', 'lunes']) }

def myTag = { attrs, body -> def msg = g.message(code: "my.localized.content", args: ['Juan', 'lunes']) }

Grails & Internationalization

Page 21: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

grails-app/conf/UrlMappings.groovy

/grails-app/conf/spring/resources.groovy

21

Grails & Internationalization

<g:link mapping="storeCategory" params="[lang:'en', category:'new']">Category</g:link>

Page 22: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails Plugin

• All available plugin– http://grails.org/plugins/?filter=all

• Plugins are themeselves Grails applications

• Plugins are provided with several hooks during install

• Plugin resources are added to the main application at startup

22

Page 23: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails Plugin

• > grails list-plugins• > grails install-plugin <name>• > grails plugin-info <name>• > grails create-plugin

23

Page 24: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

24

Page 25: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails Testcase

25

Page 26: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Most used Grails Command

– grails create-app [project_Name]– grails create-domain-class [qualified_domain_class_Name]– grails generate-all/view/controller [qualified_domain_class_Name]– grails install-plugin [plugin-name]– grails run-app– grails test-app– grails war– grails console

26

Page 27: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Grails and Mobile Application Development

27

1. Sencha/ExtJS (www.sencha.com)

2. Grails (www.grails.org)

3. MySQL (www.mysql.org)

4. Tomcat (tomcat.apache.org)

5. Apache (www.apache.org)

http://www.cubedrive.com/calendarDemo/

Page 28: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

SOA and REST with Grails

28

Page 30: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Why Grails is Popular (Cont..)

• Agile and dynamic language for the Java Virtual Machine

• Java + Additional features.• Makes modern programming features

available to Java developers with almost-zero learning curve

• Supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain

30

Page 31: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Why Grails is Popular (Cont..)

• Makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL

• •Increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications

31

Page 32: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

Why Grails is Popular (Cont..)

• Simplifies testing by supporting unit testing and mocking out-of-the-box

• Seamlessly integrates with all existing Java objects and libraries

• Compiles straight to Java byte code so you can use it anywhere you can use Java

32

Page 33: Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired.

33