330 likes | 770 Vues
Grails. Mansura Habiba 13 november 2011. Introduction. What is Grails ? Runs on the Java Virtual Machine Used Java like dynamic language Groovy Inspired by Rubi on rails Created and used by Google. Feature of Grails.
E N D
Grails Mansura Habiba 13 november 2011
Introduction What is Grails? • Runs on the Java Virtual Machine • Used Java likedynamiclanguageGroovy • InspiredbyRubi on rails • Createdandusedby Google
Feature of Grails Grails is a rapid web applicationdevelopmentframework built on Groovyand Spring Grails has full stack environement Grails is modularandextensiblethrough a plugin system Just like Spring Grailsdeployed in any environment
Stack Grails Gant (GroovyAnt) Controller Domain View (gsp) Service plugin SiteMesh Spring MVC,Beans, Txns JUnit GORM Hibernet HSQLBD Servlet Groovy Apache Common JDK JAVA EE JAVA Virtual Machine
Full Stack Web Framework Embedded Servlet An easy touse MVC Framework Simple but Powerful Web MVC GSP Viewes Servlet Container Build in DB access andModelling Build in Internationalizartion GORM (Data Access) Database I18n In Memory SQL Database Build Test Support Doc Engine WiKibased HTML Doc Engine
Grails MVC Framework Spring DispatcherServlet Extends GrailsDispatcherServlet View URI Request Simple Grails Controller URI Request Model & View Simple Grails Controller Helper User Interface (GSP) Grails Controller Grails Service/ Plug-ins DB Grails Domain Template (Sitemesh) mapping GORM
Installation (JDK) Download JDK from the followingUrl Invoke the installerand follow the installation Setup JAVA_HOME as User variable Add % JAVA_HOME %\bin toPath
Installation (Grails) • Download GGTS from the following link http://spring.io/tools/ggts • Unzip the binary • Plug andplay • No needto set up GRAILS_HOME • No needtomodifyPathvarible • No needto set up Server
Grails Installation Folder IDE Grails Server Open Source Licence
Grails Web Application: Urlmapping Custome DSL forMappingURLsto view, controller, action Support richconstraintsformechanism Support HTTP methodfor REST
Grails & validation 1 <g:hasErrorsbean="${user}"> <ul> <g:eachError var="err" bean="${user}"> <li>${err}</li> </g:eachError> </ul> </g:hasErrors> 2 <div class="fieldcontain${hasErrors(bean: userInstance, field: 'firstname', 'error')} "> <label for="firstname"> <g:messagecode="user.firstname.label" default="Firstname" /> </label> <g:textFieldname="firstname" value="${userInstance?.firstname}" /> </div> 3 <g:renderErrorsbean="${user}" /> class User { String login String password String email Integerage DatebirthDate staticconstraints = { login size: 5..15, blank: false, unique: true password size: 5..15, blank: false email email: true, blank: false agemin: 18 birthDate max: new Date() } }
Validating the data : Standerd validation • min, max -Minimum, maximum value for a numericfield • minSize, maxSizeMinimum, maximum length for a textfield • matches -Applies a regularexpressionagainsta string value • validator -Set to a closure or block to use as a customvalidation nullable - Ensures the property value cannot benull blank-Prevents empty fields email - Checks for well-formed email addresses inList -Displays a combo-box Unique -Prevents duplicate values in the database
Grails & Internationalization Default • Grailsshipswithseveralmessagebundlesfor a whole range of languageswithin the grails-app/i18n directory. For example: • messages.properties • messages_da.properties • messages_de.properties • messages_es.properties • messages_fr.properties
Grails & Internationalization <g:message code="my.localized.content" args="${ ['Juan', 'lunes'] }" /> def show() { defmsg = message(code: "my.localized.content", args: ['Juan', 'lunes']) } defmyTag = { attrs, body -> defmsg = g.message(code: "my.localized.content", args: ['Juan', 'lunes']) } Reading Messages in the View Reading Messages in Controllers Reading Messages in Tag Libraries
Grails & Internationalization <g:linkmapping="storeCategory"params="[lang:'en', category:'new']">Category</g:link> grails-app/conf/UrlMappings.groovy /grails-app/conf/spring/resources.groovy
GrailsPlugin • Allavailableplugin • 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
GrailsPlugin > grails list-plugins > grailsinstall-plugin <name> > grailsplugin-info <name> > grailscreate-plugin
Most usedGrailsCommand • grailscreate-app [project_Name] • grailscreate-domain-class [qualified_domain_class_Name] • grailsgenerate-all/view/controller [qualified_domain_class_Name] • grailsinstall-plugin [plugin-name] • grails run-app • grails test-app • grails war • grails console
Grailsand Mobile Application Development Sencha/ExtJS (www.sencha.com) Grails (www.grails.org) MySQL (www.mysql.org) Tomcat (tomcat.apache.org) Apache (www.apache.org) http://www.cubedrive.com/calendarDemo/
WhyGrails is Popular InfoQ's Top 20 Web Frameworks for the JVM 2013
WhyGrails 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
WhyGrails 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
WhyGrails 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