copyright dr peter lappo copy only with credits. 1 building an online conference booking programme...

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

Post on 19-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Copyright Dr Peter LappoCopy only with credits.

1

Building an Online Conference Booking Programme with Grails

In an Hour!

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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!

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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”.

Copyright Dr Peter LappoCopy 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

Copyright Dr Peter LappoCopy 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

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy only with credits.

13

Create The Application and Domain Classes

• Create the applicationgrails create-app conferencecd conference

• Start eclipse and import project• Disable eclipse groovy compile• Create domain classes

grails create-domain-class Conferencegrails create-domain-class ConferenceDelegategrails create-domain-class AdminUser

Copyright Dr Peter LappoCopy 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

Copyright Dr Peter LappoCopy only with credits.

15

Create Controllers

• Create the controllers.grails create-controller Conferencegrails create-controller ConferenceDelegategrails create-controller AdminUser

• Edit all to enable scaffolding.def scaffold = true

• Can generate all scaffolding code. Less flexible during development.grails generate-all

Copyright Dr Peter LappoCopy 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 browserhttp://localhost:8080/conference/

Copyright Dr Peter LappoCopy 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() } }

Copyright Dr Peter LappoCopy 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

<%response.sendRedirect(request.getContextPath()+"/conference/show/1");%>

Copyright Dr Peter LappoCopy 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

Copyright Dr Peter LappoCopy 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

<%response.sendRedirect(request.getContextPath()+"/conference/display/1");%>

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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!

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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') }

Copyright Dr Peter LappoCopy 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

Copyright Dr Peter LappoCopy only with credits.

26

Base Controller

• Add a base controller that will be used by all controllersgrails-app/controllers/BaseController.groovy

abstract class BaseController {

def auth() {

if(!session.userId) {

redirect(controller:'adminUser',action:'login')

return false

}

}

}

Copyright Dr Peter LappoCopy 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.groovyclass ConferenceDelegateController extends BaseController { def beforeInterceptor = [action:this.&auth, except:['create', 'save', 'show']]

• grails-app/controllers/ConferenceController.groovyclass ConferenceController extends BaseController { def beforeInterceptor = [action:this.&auth, except:['display', 'register']]

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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.

Copyright Dr Peter LappoCopy 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/

presentations/Getting_Started_with_Grails.pdf