• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Grails Simple Login

Grails Simple Login






Total Views
Views on SlideShare
Embed Views



23 Embeds 467

http://www.slideshare.net 236
http://swdevlittleinfos.blogspot.com 100
http://swdevlittleinfos.blogspot.gr 43
http://pradeepkumarasiri.blogspot.com 28
http://www.e-presentations.us 27
http://pradeepkumarasiri.blogspot.in 5
http://swdevlittleinfos.blogspot.in 3
http://static.slidesharecdn.com 3
http://swdevlittleinfos.blogspot.se 2
http://pinterest.com 2
http://ig.gmodules.com 2
http://swdevlittleinfos.blogspot.fr 2
http://swdevlittleinfos.blogspot.com.au 2
https://twitter.com 2
http://swdevlittleinfos.blogspot.nl 2
http://swdevlittleinfos.blogspot.be 1
http://pradeepkumarasiri.blogspot.mx 1
http://www.ig.gmodules.com 1
http://swdevlittleinfos.blogspot.de 1
http://translate.googleusercontent.com 1
http://twitter.com 1
http://webcache.googleusercontent.com 1
http://swdevlittleinfos.blogspot.ca 1


Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.


110 of 21 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • I still dont believe much the use of grails in the corporate market. but ill try to study more to see the advantages against jsf and related technologies. I m a big fan of Groovy wich one that doesnt like me is this Rails crap...
    Are you sure you want to
    Your message goes here
  • Very nice clearly understood. Have to start the practice now...
    Thanks for the useful information.
    Are you sure you want to
    Your message goes here
  • nice slide, thanks
    Are you sure you want to
    Your message goes here
  • thanks, very helpful
    Are you sure you want to
    Your message goes here
  • very good tutorial for novice like me...c:
    Are you sure you want to
    Your message goes here

110 of 21 previous next

Post Comment
Edit your comment

    Grails Simple Login Grails Simple Login Presentation Transcript

    • Grails tutorial Simple login/logout http://khomkrit.wordpress.com
    • Part I Create View
    • Prepare Artifact Create Grails Application > grails create-app simple-login and Create a Controller named user, used to handle user activity such as login and logout. > grails create-controller user
    • Prepare Artifact Create a file named “index.gsp” in grails/views/user/ Directory
    • Custom Login Page Custom the “index.gsp” file created recently
    • Custom Login Page Custom the “index.gsp” file created recently add some style Create HTML textfield for username/password
    • Custom Login Page create a form using <g:form> tag
    • Custom Login Page if you don’t want to use <g:form> tag you can use HTML form instead! Because in finally, Grails will generate plain HTML form to you. I prefer to use <g:form> tag, it’s make sense and give more semantic and info about what controller and what action will accepts from form request. <g:form action=”login”/>
    • Custom Login Page <g:form action=”login”/> send form request to action named “login” and What Controller? If you don’t specify a controller name it will send request to default controller corresponding with the name of file directory. In this situation, index.gsp is in /grails-app/ user/ directory. So, It will send request to user controller.
    • Custom Login Page <g:form controller=”user” action=”login”/> Anyway, You can add the specific controller to which you want to sent request.
    • Start Grails Application by using this command > grails run-app Go to http://localhost:8080/simple-login/user
    • Now, you cannot do anything with the form. If you click the Login button, the error will occurs. Why? Because the target action specified by you in the previous is not created.
    • What is the action specified by you? Back to the login.gsp you will see this...
    • What is the action specified by you? Back to the login.gsp you will see this... Yes, action named “login” will get the form request.
    • Part II Let’s create the Action
    • Tip You can make most changes, including changes to the domain and controller classes, without having to restart the web server. Sometimes, however, change require a restart. Creating a new controller sometimes requires a restart
    • Let’s create the action All action is declared in the controller file. In this case, you want to send request to controller named “user” and action named “login” go to UserController.groovy to create the “login” action.
    • Custom Controller all Controller is in /grails-app/controllers/ directory
    • Custom Controller Open UserController.groovy and Create an action named “login”
    • Custom Controller Go to http://localhost:8080/simple-login/user/ and Click the Login Button
    • Custom Controller OOP! Grails sent 404 back! Why?
    • Custom Controller By Default, When you call any action, Grails try to search for a file which has name corresponding with the action name called by client. In this case you try to http:/ /localhost:8080/ simple-login/user/login, this url means you try to call action named “login” in the user controller So, Grails try to find a file named “login.gsp” in / grails-app/views/user/ directory for render to client. you don’t have that file!
    • Custom Controller You don’t have the login.gsp file for Grails, It’s make Grails don’t know what should to render back to client. You can use “render” method for tell Grails what it should to render back.
    • Custom Controller Go to http://localhost:8080/simple-login/user/ and Click the Login Button again.
    • Custom Controller Good Responding :)
    • Custom Controller You can add some your logic to handle login such as keep login status in the session and send some message back to user like “you have logged in” or “your login failed” in the login action. You can add some logic to handle login here
    • Part III Add some Logic to Handle the Login
    • Validation Add simple validation to check username and password sent from client all parameters sent from client is stored in variable named “params” So, the code will look like this...
    • Validation For testing, go to login page and try to login again. If username is “admin” and password is “pass” then you can login, else cannot.
    • Validation Don’t You want to use ugly responding when user try to login? Next, After user has logged in, the Grails application will redirect user to the login page again and give some info using “flash” variable. About flash scope is out of this tutorial topic but you can investigate that how to use flash through this tutorial.
    • Using flash use flash variable to store some message as the following code and redirect to the login page (index.gsp) .
    • Using flash redirecting user to action named “index” .By default, Grails will search for file named “index.gsp” in grails-app/views/user/ directory. flash message sent to action named “index” by the last command in login action and also through to index.gsp So, You can access the flash message in action named “index” and also access it in “index.gsp”
    • Using flash Add ${flash.message} in login page If it has something in, it will show message To display value in a variable you can use this pattern ${variable-name}
    • Using flash Testing, Go to login page and try to login again. After try to login, you will see value in flash.message sent from login action.
    • Keep Status You can use “session” to keep information about login status. Keep some info by using session variable
    • Handle Logout Create a new action named “logout” to handle when user want to logout. session.user keep status that the user has logged in, so you should clear all info about logged in user. New action
    • Custom Login page Add some logic more, if user has logged in then do not show login form and show a logout link instead. Use <g:if>, <g:else> tag to test something.
    • Custom Login page Using <g:if> tag <g:if test=”${boolean-logic}”> Example: <g:if test=”${5 == 6}”>
    • Custom Login page Creating a link to logout action, you can use <g:link> as the following.
    • Custom Login page Anyway, you can use HTML link tag - <a href=””> , instead. But I prefer to use <g:link> with the same reason of why I prefer to use <g:form> <g:link controller=”” action=””> If you do not specify controller name, Grails will automatic search for controller like <g:form> behavior. <g:link action=””>
    • Check Point! Now you have a login page. If you login succeed, You can see a logout link and the login form has thrown away. If you login fail, the login form still be with you. After you try to login, Whatever the result is fail or succeed have you got. You always see a flash message.
    • Add some more thing Grails has a default CSS and you can use its like the following. Use build in CSS, class=”message”
    • Add some more thing As you can see, the style has applied to flash.message value
    • Add some more thing For another style, you can see in the /web- app/css/ directory Other resource is in /web-app/images/ and web-app/js directory
    • Q/A Have any Question?
    • Thank You Sorry about many wrong grammar in this slide :D