扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:山风小子 来源:蛟龍居 2007年11月18日
关键字:
在本页阅读全文(共4页)
4,在“命令行”中,输入“grails create-controller Login”,创建登陆(login)的控制器LoginController,它对应MVC中的Controller,让它负责登陆相关业务,输出如下:
D:\Temp\grails_apps\demo>grails create-controller Login Welcome to Grails 0.4.2 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: D:\D\MY_DEV\grails Base Directory: D:\Temp\grails_apps\demo Environment set to production Running script D:\D\MY_DEV\grails\scripts\CreateController.groovy [mkdir] Created dir: D:\Temp\grails_apps\demo\grails-app\views\login [copy] Copying 1 file to D:\Temp\grails_apps\demo\grails-app\controllers Created Controller at D:\Temp\grails_apps\demo/grails-app/controllers/LoginContr oller.groovy [copy] Copying 1 file to D:\Temp\grails_apps\demo\grails-tests Created ControllerTests at D:\Temp\grails_apps\demo/grails-tests/LoginController Tests.groovy D:\Temp\grails_apps\demo> |
5,修改demo\grails-app\controllers\LoginController.groovy的内容为:
class LoginController { def loginService def index = { } def login = { User u = new User() u.properties = params if (params.name && params.password) { def user = loginService.check(u) if (user) { flash.message = "Welcome ${user.name}" render(view: "ok", model: [user: user]) } else { flash.error = "Invalid ${u.name} with ${u.password}" render(view: "login", model: [user: u]) } } else { render(view: "login", model: [user: u]) } } } |
大家或许也看到了LoginService这个类,我将在后面演示创建它,这个LoginService类封装了所有登陆相关的业务逻辑,Grails自动将其注入到LoginController中。
6,在“命令行”中,输入“grails create-service Login”,创建LoginService.groovy,输出:
D:\Temp\grails_apps\demo>grails create-service Login Welcome to Grails 0.4.2 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: D:\D\MY_DEV\grails Base Directory: D:\Temp\grails_apps\demo Environment set to production Running script D:\D\MY_DEV\grails\scripts\CreateService.groovy [copy] Copying 1 file to D:\Temp\grails_apps\demo\grails-app\services Created Service at D:\Temp\grails_apps\demo/grails-app/services/LoginService.gro ovy [copy] Copying 1 file to D:\Temp\grails_apps\demo\grails-tests Created ServiceTests at D:\Temp\grails_apps\demo/grails-tests/LoginServiceTests. groovy D:\Temp\grails_apps\demo> |
7,修改demo\grails-app\services\LoginService.groovy的内容为:
class LoginService { boolean transactional = true def check(User u) { def user = User.findWhere(name: u.name, password: u.password) return user } def serviceMethod() { // TODO } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者