Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright Dr Peter Lappo Copy only with credits. 1 Building an Online Conference Booking Programme with Grails In an Hour !

Similar presentations


Presentation on theme: "Copyright Dr Peter Lappo Copy only with credits. 1 Building an Online Conference Booking Programme with Grails In an Hour !"— Presentation transcript:

1 Copyright Dr Peter Lappo Copy only with credits. 1 Building an Online Conference Booking Programme with Grails In an Hour !

2 Copyright Dr Peter Lappo Copy only with credits. 2 Inspiration This talk was inspired by a friend who was given a very challenging deadline which would have been impossible to achieve using traditional tools decided to use Grails and delivered a solution on time.

3 Copyright Dr Peter Lappo Copy only with credits. 3 Metrics Approx 20 hours were spent learning Groovy, Grails, creating the application and writing the presentation. I am indebted to Jason Rudolph the writer of “Getting Started with Grails”. Without his book this presentation would have taken much longer to prepare. Obviously I am not an expert in Groovy or Grails! I wouldn’t call myself a web developer!

4 Copyright Dr Peter Lappo Copy only with credits. 4 What is Grails? A program to create web applications quickly and easily. MVC Architecture (more later): –Model –View –Controllers Inspired by Ruby on Rails, but uses Groovy and Java.

5 Copyright Dr Peter Lappo Copy only with credits. 5 Main Features Convention over Configuration. DRY - Don’t Repeat Yourself. Strong Java integration. MVC - more on this later. Code can be modified without restarting the application - agility.

6 Copyright Dr Peter Lappo Copy only with credits. 6 How to use Grails? Usage: –Standalone –Integrated with a web server e.g. Tomcat Just create domain classes and database tables, controllers and views happen by “magic”.

7 Copyright Dr Peter Lappo Copy only with credits. 7 Foundations Groovy scripting language –powerful scripting language with first class Java integration –Groovy compiles into Java byte code Use any Java classes easily Spring (Inversion Of Control) - used for configuration - easily reused from other apps Hibernate - powerful persistence layer - happens by “magic” i.e. convention over configuration Sitemesh - to layout pages

8 Copyright Dr Peter Lappo Copy only with credits. 8 Install Java 1.6/1.5 Groovy 1.5.1 Grails 1.0.1 Eclipse 3.3.1.1 - optional –can use Intellij, Netbeans or a text editor

9 Copyright Dr Peter Lappo Copy only with credits. 9 Model View Controller (MVC) Model –Domain objects – map to database tables. –Constraints - validate fields and order the view. –Dynamic - change domain - changes database. –Dynamic Finders - not static - automatically generated from finder method name - v.cool. View –Template to display domain objects. –Can use gsp, jsp or html. –Tag libraries - easy to use - can change dynamically. Controller –Decides what to do when a view submits and action to the server. –“Flash” feature - for messages in the view.

10 Copyright Dr Peter Lappo Copy only with credits. 10 Scaffolding Code Generation –Generates code on the fly using domain objects. –Can generate code statically useful as can learn from generated code. –This exercise will delay code generation as long as possible.

11 Copyright Dr Peter Lappo Copy only with credits. 11 Other Features Services –To provide access to messaging layers for instance. Tag libs –To extend gsp tags for your application. Plugins –To extend Grails. AJAX or Web 2.0 integration. All open source and free.

12 Copyright Dr Peter Lappo Copy only with credits. 12 Booking Programme Requirements Workflow –Any User Front page - One Conference - read only. Register as a delegate. Unable to see more without login. –Admin User Login. List of Delegates. Full create/read/update/delete access to the application.

13 Copyright Dr Peter Lappo Copy only with credits. 13 Create The Application and Domain Classes Create the application grails create-app conference cd conference Start eclipse and import project Disable eclipse groovy compile Create domain classes grails create-domain-class Conference grails create-domain-class ConferenceDelegate grails create-domain-class AdminUser

14 Copyright Dr Peter Lappo Copy only with credits. 14 Model The Domain Import Eclipse project We will cheat a bit here. Copy definitions from finished project. –grails-app/domain/ConferenceDelegate.groovy –grails-app/domain/Conference.groovy –grails-app/domain/AdminUser.groovy

15 Copyright Dr Peter Lappo Copy only with credits. 15 Create Controllers Create the controllers. grails create-controller Conference grails create-controller ConferenceDelegate grails create-controller AdminUser Edit all to enable scaffolding. def scaffold = true Can generate all scaffolding code. Less flexible during development. grails generate-all

16 Copyright Dr Peter Lappo Copy only with credits. 16 Run It! Run the application. This starts a web server, a database (HSQL) and the grails application. grails run-app See what it does in the browser http://localhost:8080/conference/

17 Copyright Dr Peter Lappo Copy only with credits. 17 Initialise The Domain In this example there is only one conference and one admin user so we’ll initialise them at startup and the database is dropped when the application stops. –Note the dynamic finders In grails-app/conf/BootStrap.groovy, add def init = { servletContext -> // create the admin login user final String ADMIN = 'admin' if (!AdminUser.findByName(ADMIN)) { new AdminUser(name:ADMIN,password:ADMIN).save() } // create the conference - can be done via the web interface final String CONF = 'ECA 2007' if (!Conference.findByTitle(CONF)) { // 10:30 July 22 2008 def cal = new GregorianCalendar(2008, Calendar.JULY, 22, 10, 30) new Conference(title:CONF,date:cal.time).save() }

18 Copyright Dr Peter Lappo Copy only with credits. 18 New Front Page Restart, and see the added data. grails run-app Redirect front page to the conference page Change web-app/index.jsp

19 Copyright Dr Peter Lappo Copy only with credits. 19 Customise the Display Page grails generate-view Conference (to get an example) Copy show.gsp to display.gsp and –Remove Home, Conference List, New Conference, Edit, Delete –Add register button to take you to the new ConferenceDelegate page –Change the text to "Welcome to Our Conference“ –Remove the ID from the display to make it more friendly. Cheat: grails-app/views/conference/display.gsp

20 Copyright Dr Peter Lappo Copy only with credits. 20 Customise the Display Page (2) Change the controller as don't forget this is a MVC architecture grails-app/controllers/ConferenceController.groovy def display = { [ conference : Conference.get( params.id ) ] } Change the front page web-app/index.jsp

21 Copyright Dr Peter Lappo Copy only with credits. 21 Custom Register Page Implement a custom register page, again without the editing features Need a register closure in the controller grails- app/controllers/ConferenceController.groovy def register = { redirect(controller:'conferenceDelegate', action:'create') } Test it.

22 Copyright Dr Peter Lappo Copy only with credits. 22 Progress So Far Have an application to manage conferences and delegates. Very little programming. No database programming. But, too open for public use. Need security. Non-programmers may fall asleep here!

23 Copyright Dr Peter Lappo Copy only with credits. 23 Security Model For this application we’ll implement a very simple security model. Any access to public pages. Restricted access to admin pages. Only one user, the admin user. Delegates just register and email is used to authenticate and confirm registration. –This bit has not been implemented.

24 Copyright Dr Peter Lappo Copy only with credits. 24 Admin User Controller grails-app/controllers/AdminUserController.groovy def login = { if (request.method == "GET") { session.userId = null def user = new AdminUser() } else { def user = AdminUser.findByNameAndPassword(params.name, params.password) if (user) { session.userId = user.name redirect(uri:'/') redirect(controller:'conferenceDelegate') } else { flash['message'] = 'Please enter a valid user name and password' } def logout = { session.userId = null flash['message'] = 'Successfully logged out' redirect(controller:'conference', action:'display', id:'1') }

25 Copyright Dr Peter Lappo Copy only with credits. 25 Login Template Add (another cheat) grails-app/views/adminUser/login.gsp grails run-app http://localhost:8080/conference/adminUser/login http://localhost:8080/conference/adminUser/logout But can still access anything else without permission. Restart, and test the changes. grails run-app

26 Copyright Dr Peter Lappo Copy only with credits. 26 Base Controller Add a base controller that will be used by all controllers grails-app/controllers/BaseController.groovy abstract class BaseController { def auth() { if(!session.userId) { redirect(controller:'adminUser',action:'login') return false }

27 Copyright Dr Peter Lappo Copy only with credits. 27 Use the Base Controller grails-app/controllers/AdminUserController.groovy class AdminUserController extends BaseController { def beforeInterceptor = [action:this.&auth, except:['login', 'logout']] grails-app/controllers/ConferenceDelegateController.groovy class ConferenceDelegateController extends BaseController { def beforeInterceptor = [action:this.&auth, except:['create', 'save', 'show']] grails-app/controllers/ConferenceController.groovy class ConferenceController extends BaseController { def beforeInterceptor = [action:this.&auth, except:['display', 'register']]

28 Copyright Dr Peter Lappo Copy only with credits. 28 That’s it Folks Run the application. See its limitations, but it does work. Can be improved by customising the pages further but will stop here as it meets the requirements.

29 Copyright Dr Peter Lappo Copy only with credits. 29 Conclusion What have we learnt? –Minimal effort to create a web application. –Framework allows the developer to change the application without restarting. –Conventions simplify things. –Deferring code generation keeps development flexible at the expense of customisation.

30 Copyright Dr Peter Lappo Copy only with credits. 30 References Grails website –http://grails.org/ Groovy website –http://groovy.codehaus.org/ Getting Started with Grails PDF book –http://jasonrudolph.com/downloads/presentati ons/Getting_Started_with_Grails.pdf


Download ppt "Copyright Dr Peter Lappo Copy only with credits. 1 Building an Online Conference Booking Programme with Grails In an Hour !"

Similar presentations


Ads by Google