Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual...
-
Upload
james-walters -
Category
Documents
-
view
214 -
download
0
Transcript of Grails Mansura Habiba 13 november 2011. Introduction What is Grails? Runs on the Java Virtual...
Grails
Mansura Habiba13 november 2011
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
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
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
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
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
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
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
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
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
Grails Installation Folder
11
IDE
Grails
Server Open Source Licence
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
Grails Web Application: Important files
13
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
15
Grails Web Application: Url mapping
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
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
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
19
Grails & Internationalization
• 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
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>
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
Grails Plugin
• > grails list-plugins• > grails install-plugin <name>• > grails plugin-info <name>• > grails create-plugin
23
24
Grails Testcase
25
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
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/
SOA and REST with Grails
28
Why Grails is Popular
29
InfoQ's Top 20 Web Frameworks for the JVM 2013
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
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
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
33