groovy & grails - lecture 9
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

groovy & grails - lecture 9

on

  • 1,839 views

Eclipse tips: tasks

Eclipse tips: tasks
Productive developer tips: technical debt
Web applications
Grails
Twitter application: step 1

Statistics

Views

Total Views
1,839
Views on SlideShare
1,835
Embed Views
4

Actions

Likes
0
Downloads
18
Comments
0

2 Embeds 4

http://www.slideshare.net 3
https://duckduckgo.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • new round starting web application\nGoal: to build a twitter like application\ntoday is an introduction to web application\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Pervert use of task to remember what you want to remember\n!!Moderate use of FIXME\n\n
  • Pervert use of task to remember what you want to remember\n!!Moderate use of FIXME\n\n
  • Pervert use of task to remember what you want to remember\n!!Moderate use of FIXME\n\n
  • ctrl F7\nof lower left button to open a view\nclose you projects\nview is just one box, a perspective (upper right corner) is a arrangement of views\nTasks on server: trac, mantis tickets (bug, features etc.)\n
  • ctrl F7\nof lower left button to open a view\nclose you projects\nview is just one box, a perspective (upper right corner) is a arrangement of views\nTasks on server: trac, mantis tickets (bug, features etc.)\n
  • ctrl F7\nof lower left button to open a view\nclose you projects\nview is just one box, a perspective (upper right corner) is a arrangement of views\nTasks on server: trac, mantis tickets (bug, features etc.)\n
  • Example: your parse SwissProt from text format for getting AC and sequence, then, your are asked to read for splicing info => you text parsing get more and more complex\nJumping to xml parsing is an investment (you loose one day coming back to the same point), but you’ll move faster further on.\n
  • Example: your parse SwissProt from text format for getting AC and sequence, then, your are asked to read for splicing info => you text parsing get more and more complex\nJumping to xml parsing is an investment (you loose one day coming back to the same point), but you’ll move faster further on.\n
  • Example: your parse SwissProt from text format for getting AC and sequence, then, your are asked to read for splicing info => you text parsing get more and more complex\nJumping to xml parsing is an investment (you loose one day coming back to the same point), but you’ll move faster further on.\n
  • html javascript css form\nanchor query argument\nGET/POST\nimages\nservlet CGI\najax flash\napache / tomcat\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Application is not a weekly TP just enforced by a series of test\nThe application must go public, to be used by the (lab) customer\nFind the easiest way to deploy you app\n
  • Install groovy, copy jars\nCall the scripts => teach your user\nWindows is catastrophic for command line\nHowever, script can be the solution for high volumes (biologist can learn...)\n
  • Griffon (swing based)\nStill need to install software on customer machine\nSoftware update to take into account (JNLP is a solution)\nFull access to the user’s machine\nStandalone solution\n
  • GUI: Graphical User Interface\nEveryone has a browser\nnetwork is fast\nExample: gmail, google map etc.\nJavascript/Ajax turn the browser into a user friendly experience\nFlash: little limits compared to a native GUI (not on the iPhone/iPad family...). Can grant acces to the host computer resources\nEasy to deploy (everyone’s connected)\nEasy updates\nCan install a server on the customer machines\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • All the web application architecture is ready\nConfiguration at it simplest level (convention over configuration)\ndomain (database stored beans)\nplugin oriented to enrich the app\nSTS integration\n
  • High changing pace in the versions, keep up to date...\n
  • High changing pace in the versions, keep up to date...\n
  • High changing pace in the versions, keep up to date...\n
  • High changing pace in the versions, keep up to date...\n
  • High changing pace in the versions, keep up to date...\n
  • Version field is set automatically\n
  • a very common command call, so better configure it as a simple shortcut\n
  • a very common command call, so better configure it as a simple shortcut\n
  • a very common command call, so better configure it as a simple shortcut\n
  • equivalent to command line\nexport GRAILS_HOME=/path/to/installed/grails-x.y.z\nexport PATH=$PATH:$GRAILS_HOME/bin\ngrails create-app eop.lec9.demo\n
  • + some optional directories\ntoday : only controller & view\n“we’ll see that later” will be the most commonly used word today...\n\n
  • + some optional directories\ntoday : only controller & view\n“we’ll see that later” will be the most commonly used word today...\n\n
  • Port 80 by default http://localhost\nPort <1024 traditionally reserved to root\nModifying the code (to some extent) does not require to re-start the server (wait 2 seconds)\nIt is not possible to launch twice the application (port conflict). Must kill before relaunching if ever\n
  • Port 80 by default http://localhost\nPort <1024 traditionally reserved to root\nModifying the code (to some extent) does not require to re-start the server (wait 2 seconds)\nIt is not possible to launch twice the application (port conflict). Must kill before relaunching if ever\n
  • Port 80 by default http://localhost\nPort <1024 traditionally reserved to root\nModifying the code (to some extent) does not require to re-start the server (wait 2 seconds)\nIt is not possible to launch twice the application (port conflict). Must kill before relaunching if ever\n
  • Versions are displayed (grails, your application)\nList of plugins used by default (we’ll add some more later)\n
  • \n
  • G create-controller foo.hello \nDo not forget to refresh eclipse after each command line action (F5) or right-button > refresh\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Test is ready, but we’re not yet there!\n
  • \n
  • Convention is the naming of file, instead of description in configuration files + class inheritance\n
  • Convention is the naming of file, instead of description in configuration files + class inheritance\n
  • Convention is the naming of file, instead of description in configuration files + class inheritance\n
  • Convention is the naming of file, instead of description in configuration files + class inheritance\n
  • index is by default (we’ll see the configuration later)\n
  • index is by default (we’ll see the configuration later)\n
  • No need to relaunch the application when you add action or controllers\n\n
  • No need to relaunch the application when you add action or controllers\n\n
  • \n
  • \n
  • \n
  • Before going to more complex action (we have not talked about parameters). let’s make a tour on view\nText is maybe not always enough\nWhat do you know of html?\n\n
  • \n
  • This is a little bit too simplistic, as we can have in practice one GSP for multiple actions, or an action can be redirected towards one or another view depending on the context\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • actions directText & withDate do not have .gsp as they render directly text\n
  • actions directText & withDate do not have .gsp as they render directly text\n
  • \n
  • ${...} extrapolate groovy expression (just as in a println statement)\nNote the link back to home page\nOf course, it is possible to pass more than one variable in the returned map\n
  • ${...} extrapolate groovy expression (just as in a println statement)\nNote the link back to home page\nOf course, it is possible to pass more than one variable in the returned map\n
  • \n
  • \n
  • Parameters are passed as map of string\nNote that we can still access params within the gsp\n
  • Parameters are passed as map of string\nNote that we can still access params within the gsp\n
  • Parameters are passed as map of string\nNote that we can still access params within the gsp\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • One of the most commons software architecture design pattern\nShare responsibility among each components\nModel is far more than just some data exchanged between the controller and the view (we see that later)\n\n

groovy & grails - lecture 9 Presentation Transcript

  • 1. Groovy: Efficiency Oriented ProgrammingLecture 9Master Proteomics & Bioinformatics - University of GenevaAlexandre Masselot - summer 2011
  • 2. Contents
  • 3. Contents‣ Eclipse tips: tasks
  • 4. Contents‣ Eclipse tips: tasks‣ Productive developer tips: technical debt
  • 5. Contents‣ Eclipse tips: tasks‣ Productive developer tips: technical debt‣ Web applications
  • 6. Contents‣ Eclipse tips: tasks‣ Productive developer tips: technical debt‣ Web applications‣ Grails
  • 7. Contents‣ Eclipse tips: tasks‣ Productive developer tips: technical debt‣ Web applications‣ Grails‣ Twitter application: step 1
  • 8. Eclipse tips
  • 9. Eclipse tips‣ Annotate code with tasks
  • 10. Eclipse tips‣ Annotate code with tasks‣ TODO / FIXME
  • 11. Eclipse tips‣ Annotate code with tasks‣ TODO / FIXME‣ Add comments into your code void mutate(){ //TODO check that mutation **does** modify position board.piecesPositions[ randomGenerator.nextInt(board.maxPieces)] = [ randomGenerator.nextInt(board.size), randomGenerator.nextInt(board.size) ] }
  • 12. Eclipse tips (cont’d)
  • 13. Eclipse tips (cont’d)‣ Lines is prefixed with an icon
  • 14. Eclipse tips (cont’d)‣ Lines is prefixed with an icon‣ The task view
  • 15. Technical debt
  • 16. Technical debt‣ Increasing the debt: - poor code - repeat in code/process (DRY) - postponing refactoring - skipping tests
  • 17. Technical debt‣ Increasing the debt: - poor code - repeat in code/process (DRY) - postponing refactoring - skipping tests‣ Interest are expensive
  • 18. Technical debt‣ Increasing the debt: - poor code - repeat in code/process (DRY) - postponing refactoring - skipping tests‣ Interest are expensive‣ A common solution: use prototyping
  • 19. What do you know about www?
  • 20. A simple (static) web query
  • 21. protocol
  • 22. protocol server host
  • 23. protocol server host resource path
  • 24. 1 find the server (DNS)
  • 25. 1 find the server (DNS)2 pass the local resource requested
  • 26. 1 find the server (DNS)2 pass the local resource requested3 server loads the resource to be served
  • 27. 1 find the server (DNS)2 pass the local resource requested3 server loads the resource to be served4 resource sent back to the browser (client)
  • 28. 1 find the server (DNS)2 pass the local resource requested3 server loads the resource to be served4 resource sent back to the browser (client)5 browser interprets the response
  • 29. 1 find the server (DNS)2 pass the local resource requested3 server loads the resource to be served4 resource sent back to the browser (client)5 browser interprets the response6 browser loads additional resources (+ requests)
  • 30. 1 find the server (DNS)2 pass the local resource requested3 server loads the resource to be served4 resource sent back to the browser (client)5 browser interprets the response6 browser loads additional resources (+ requests)7 browser displays results
  • 31. Static is not alway enough dynamic url queries
  • 32. An action is performed on the server side parameter are passed
  • 33. action path
  • 34. action path parameter(s)
  • 35. action path parameter(s)Instead of a static resource, the results is computed
  • 36. Master your browser!Today: firefox (idem for chrome, safari, ~IE)
  • 37. View page source(best inspiration is by copying others)
  • 38. Install firebug & web developer add-ons
  • 39. display loaded resourcesfirebug > network
  • 40. static page
  • 41. dynamic resource + interactions
  • 42. More operations can be undertaken within pageBut cycle query->server->rendering is always respected
  • 43. More operations can be undertaken within pageBut cycle query->server->rendering is always respected
  • 44. Why using/building web based applications?
  • 45. A script does not suit everyone (unfortunately)
  • 46. Classic deploy: window based application
  • 47. A web browser: a powerful GUI
  • 48. A web transaction
  • 49. A web transaction‣ Url request - http://host:8080/entry/show?ac=P12345&format=fasta
  • 50. A web transaction‣ Url request - http://host:8080/entry/show?ac=P12345&format=fasta‣ Action - build the model
  • 51. A web transaction‣ Url request - http://host:8080/entry/show?ac=P12345&format=fasta‣ Action - build the model‣ View generation - html contents creation
  • 52. A web transaction‣ Url request - http://host:8080/entry/show?ac=P12345&format=fasta‣ Action - build the model‣ View generation - html contents creation‣ Rendering
  • 53. A web transaction‣ Url request [browser] - http://host:8080/entry/show?ac=P12345&format=fasta‣ Action [server] - build the model‣ View generation [server] - html contents creation‣ Rendering [browser]
  • 54. Groovy + web = Grails
  • 55. Grails with STS eclipse
  • 56. Grails with STS eclipse‣ Help > dashboard > extensions > Grails
  • 57. Grails with STS eclipse‣ Help > dashboard > extensions > Grails‣ Download Grails - http://grails.org/Download
  • 58. Grails with STS eclipse‣ Help > dashboard > extensions > Grails‣ Download Grails - http://grails.org/Download‣ Uncompress in local directory - e.g. ~/local/grails-1.3.7
  • 59. Grails with STS eclipse‣ Help > dashboard > extensions > Grails‣ Download Grails - http://grails.org/Download‣ Uncompress in local directory - e.g. ~/local/grails-1.3.7‣ Configure Grails in STS preferences - possible to host multiple versions
  • 60. Grails with STS eclipse‣ Help > dashboard > extensions > Grails‣ Download Grails - http://grails.org/Download‣ Uncompress in local directory - e.g. ~/local/grails-1.3.7‣ Configure Grails in STS preferences - possible to host multiple versions‣ Reference - http://grails.org/doc/latest/
  • 61. Grails with STS eclipse (cont’d)‣ Configure shortcut for grails command prompt
  • 62. Grails with STS eclipse (cont’d)‣ Configure shortcut for grails command prompt‣ Preference > keys > open grails command prompt - <ctrl> <alt> G - (maybe need to deactivate another shortcut)
  • 63. Grails with STS eclipse (cont’d)‣ Configure shortcut for grails command prompt‣ Preference > keys > open grails command prompt - <ctrl> <alt> G - (maybe need to deactivate another shortcut)‣ Calling grails command on selected project
  • 64. STS: creating a new Grails application
  • 65. STS: creating a new Grails application‣ File > new > Grails project
  • 66. New Grails project structure
  • 67. New Grails project structure%PROJECT_HOME% + grails-app + conf ---> configuration artifacts + controllers ---> controller artifacts + domain ---> domain classes + i18n ---> message bundles for i18n + services ---> services + taglib ---> tag libraries + util ---> special utility classes + views ---> views + layouts ---> layouts + lib + scripts ---> scripts + src + groovy ---> other Groovy source files + test ---> generated test classes + web-app + WEB-INF ---> static documents
  • 68. New Grails project structure%PROJECT_HOME% + grails-app + conf ---> configuration artifacts + controllers ---> controller artifacts + domain ---> domain classes + i18n ---> message bundles for i18n + services ---> services + taglib ---> tag libraries + util ---> special utility classes + views ---> views + layouts ---> layouts + lib + scripts ---> scripts + src + groovy ---> other Groovy source files + test ---> generated test classes + web-app + WEB-INF ---> static documents
  • 69. Launching the application‣ Launch the server, either: - Run As > Grails Command (run-app) - <ctrl> <alt> G run-app↵ - command line (in the project folder): run-app
  • 70. Launching the application‣ Launch the server, either: - Run As > Grails Command (run-app) - <ctrl> <alt> G run-app↵ - command line (in the project folder): run-app‣ Check console for messages
  • 71. Launching the application‣ Launch the server, either: - Run As > Grails Command (run-app) - <ctrl> <alt> G run-app↵ - command line (in the project folder): run-app‣ Check console for messages‣ With your browser: - http://localhost:8080/eop.lec9.demo
  • 72. url ➙ controller (action) ➙ render1) write code for controller/action2) write rendering (html page)
  • 73. Create a controller
  • 74. Create a controller‣ Either: - - command line: create-controller foo.hello
  • 75. New files and directories:./grails-app/controllers/foo/HelloController.groovy./test/unit/foo/HelloControllerTests.groovy./grails-app/views/hello/
  • 76. The controller/action code
  • 77. HelloController.groovypackage fooclass HelloController { def index = { }}
  • 78. HelloController.groovypackage fooclass HelloController { def index = { }}‣ hello = controller
  • 79. HelloController.groovypackage fooclass HelloController { def index = { }}‣ hello = controller‣ index = default action
  • 80. HelloController.groovy package foo class HelloController { def index = { } }‣ hello = controller‣ index = default action‣ Convention over configuration grails-app/controllers/HelloController.groovy
  • 81. Invoking the controller
  • 82. Invoking the controller http://localhost:8080/eop.lec9.demo/hello http://localhost:8080/eop.lec9.demo/hello/index‣ No rendering is defined yet:HTTP Status 404 - /eop.lec9.demo/WEB-INF/grails-app/views/hello/index.jsptype Status reportmessage /eop.lec9.demo/WEB-INF/grails-app/views/hello/index.jspdescription The requested resource (/eop.lec9.demo/WEB-INF/grails-app/views/hello/index.jsp) is not available.Apache Tomcat/6.0-snapshot
  • 83. Invoking the controller http://localhost:8080/eop.lec9.demo/hello http://localhost:8080/eop.lec9.demo/hello/index‣ No rendering is defined yet:HTTP Status 404 - /eop.lec9.demo/WEB-INF/grails-app/views/hello/index.jsptype Status reportmessage /eop.lec9.demo/WEB-INF/grails-app/views/hello/index.jspdescription The requested resource (/eop.lec9.demo/WEB-INF/grails-app/views/hello/index.jsp) is not available.Apache Tomcat/6.0-snapshot problem: we must define the view
  • 84. A simple action‣ Action = closure class HelloController { ... def directText = { render "Hello World" } }
  • 85. A simple action‣ Action = closure class HelloController { ... def directText = { render "Hello World" } }‣ http://localhost:8080/eop.lec9.demo/hello/directText Hello World
  • 86. Another simple action
  • 87. Another simple action‣ Action = closure class HelloController { ... def withDate = { def now = new Date() render "Hello World at $now" } }
  • 88. Another simple action‣ Action = closure class HelloController { ... def withDate = { def now = new Date() render "Hello World at $now" } }‣ http://localhost:8080/eop.lec9.demo/hello/withDate Hello World at Mon May 03 02:38:03 CEST 2010
  • 89. But raw test is soon a bit rough...
  • 90. Enhanced web pages = dynamic html
  • 91. Controller/action ➙ Model (data) ➙ GSP (view)
  • 92. One action ↔ one GSP view
  • 93. GSP by default‣ One controller = one directory in grails-app/views - foo.HelloController.groovy ➙ grails-app/views/hello/
  • 94. GSP by default‣ One controller = one directory in grails-app/views - foo.HelloController.groovy ➙ grails-app/views/hello/‣ One action = one .gsp file - def index = {...} ➙ grails-app/views/hello/index.gsp
  • 95. GSP by default‣ One controller = one directory in grails-app/views - foo.HelloController.groovy ➙ grails-app/views/hello/‣ One action = one .gsp file - def index = {...} ➙ grails-app/views/hello/index.gsp‣ .gsp file: - pure html - dedicated tags - model variables can be passed
  • 96. A static gsp example(content is always the same)
  • 97. hello/index.gsp
  • 98. hello/index.gsp<html> <head> <title>Hello world index page</title> </head> <body> <h1>Welcome to hello world</h1> <ul> <li><a href="/eop.lec9.demo/hello/directText"> simple text hello</a></li> <li><a href="/eop.lec9.demo/hello/withDate"> text with date</a></li> </ul> </body></html>
  • 99. Absolute path are not good practice use grails tag instead<g:link controller=”...” action=”...”>
  • 100. hello/index.gsp
  • 101. hello/index.gsp <ul> <li> <g:link controller="hello" action="directText"> simple text hello </g:link> </li> <li> <g:link controller="hello" action="withDate"> text with date </g:link> </li> </ul>
  • 102. hello/index.gsp <ul> <li> <g:link controller="hello" action="directText"> simple text hello </g:link> </li> <li> <g:link controller="hello" action="withDate"> text with date </g:link> </li> </ul>‣ Documentation:http://grails.org/doc/latest/ ➙ tags
  • 103. Dynamic gspcontent (model) is computed by the action
  • 104. Passing data to the .gsp‣ A new action returning map of values def when = { def now = new Date() return [myTime:now] }
  • 105. Passing data to the .gsp‣ A new action returning map of values def when = { def now = new Date() return [myTime:now] }‣when.gsp <body> <h1>Current time is ${myTime}</h1> We are <b>${myTime.seconds}</b> seconds past the minute <hr/> <g:link controller="hello" action="index"> back home</g:link> </body>
  • 106. Getting parameters from the url
  • 107. Passing arguments‣http://.../hello/ago?nbdays=4
  • 108. Passing arguments‣http://.../hello/ago?nbdays=4‣ Controller access parameters through map params def ago = { def now = new Date() def d = now - (params.nbdays as Integer) [currentTime:now, pastTime:d] }
  • 109. Passing arguments‣http://.../hello/ago?nbdays=4‣ Controller access parameters through map params def ago = { def now = new Date() def d = now - (params.nbdays as Integer) [currentTime:now, pastTime:d] }‣ago.gsp Current time is ${currentTime}. <br/> It was ${pastTime}, ${params.nbdays} days ago
  • 110. How to call an action with parameters?
  • 111. Passing arguments statically
  • 112. Passing arguments statically‣Through the urlhttp://.../hello/ago?nbdays=4
  • 113. Passing arguments statically
  • 114. Passing arguments statically‣ Within a <g:link> tag <li> <g:link controller="hello" action="ago" params="[nbdays:4]" >4 days ago</g:link> </li>
  • 115. Let the user set a parameter: <form>
  • 116. Passing arguments (cont’d)‣ Within a user entered <form> <g:form controller="hello" action="ago"> <!-- default value is the one received from params.nbdays --> <g:textField name="nbdays" value="${params.nbdays}" /> <g:submitButton name="submit" value="new date"/> </g:form>
  • 117. Passing arguments (cont’d)‣ Within a user entered <form> <g:form controller="hello" action="ago"> <!-- default value is the one received from params.nbdays --> <g:textField name="nbdays" value="${params.nbdays}" /> <g:submitButton name="submit" value="new date"/> </g:form>‣ The form is included here in the ago.gsp
  • 118. Passing arguments (cont’d)‣ Within a user entered <form> <g:form controller="hello" action="ago"> <!-- default value is the one received from params.nbdays --> <g:textField name="nbdays" value="${params.nbdays}" /> <g:submitButton name="submit" value="new date"/> </g:form>‣ The form is included here in the ago.gsp‣ Could be in another gsp file (with a static default value)
  • 119. Model - View - Controller