在Grails中创建一个简单的Twitter应用程序(第1部分)

介绍

学习grails,网上为您准备了很多教程。我在网上找到一个视频教程,介绍用Grails90分钟创建一个简单的Twitte应用程序,可以跟着视频一步一步地学习。但视频教程讲的非常快,可能跟不上。所以我创建这个简单的教程,供您参考。

我的假设是,你使用Grails2.0.0版本。除此之外,在本教程中我使用的插件有以下版本:searchable – 0.6.3 and springSecurityCore – 1.2.6.。 我使用的操作系统为 Ubuntu。实际上,不同版本的插件,结果可能会有不同。

 让我们开始吧

每一个Grails项目的第一步都是是创建项目。启动终端,并进入到所需的工作目录。例如,我想在我的桌面上创建的项目文件夹,首先去那里。

grails create-app simple-twitter

等待Grails的配置项目。配置完成后,进入项目所在目录,并且安装spring-security-core插件。

cd simple-twitter
grails install-plugin spring-security-core

插件将为您的项目提供登录和注销功能。安装插件后,输入下面的代码:

grails s2-quickstart org.grails.twitter Person Authority

这里的org.grails.twitter 是Person 和 Authority类所在的包。Person 拥有Authority,Authority是赋予Person 的一个角色。

您会发现,在grails-app/domain/org/grails/twitter路径下,系统为我们创建了3个领域模型:

  • Authority.groovy
  • Person.groovy
  • PersonAuthority.groovy
打开Person.groovy,让我们添加一个新字段,放在String username 之前。
String realName
看起来想这个样子:
Person.groovy
package org.grails.twitter
 
class Person {
 
    transient springSecurityService
 
    String realName
    String username
    String password
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
 
    static constraints = {
        username blank: false, unique: true
        password blank: false
    }
 
    static mapping = {
        password column: '`password`'
    }
 
    Set
    
    
     
      getAuthorities() {
        PersonAuthority.findAllByPerson(this).collect { it.authority } as Set
    }
 
    def beforeInsert() {
        encodePassword()
    }
 
    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }
 
    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}
    
    
这里只是添加了一个真实姓名字段,用户可以在其中指定他的真实姓名。
首次运行项目时,让我们创建一些虚拟的用户为我们工作。
在grails-app/conf的文件夹里面,你可以找到BootStrap.groovy中的文件。项目运行时,这个文件首先被加载到服务器上并对项目进行初始化。让我们编辑这个文件,在每一次服务器启动时,自动填充虚拟用户。
这个文件看起来想这个样子:
BootStrap.groovy
import org.grails.twitter.*
 
class BootStrap {
 
    def init = { servletContext ->
        /* If there are no Persons in the record. */
        if (!Person.count()) {
            createData()
        }
    }
    def destroy = {
    }
 
    private void createData() {
        def userRole = new Authority(authority: 'ROLE_USER').save()
 
        /* The default password for all user. No need to encode here to avoid double encoding. */
        String password = 'password'
 
        [yancy: 'Yancy Vance Paredes', john: 'John Doe', jane: 'Jane Smith'].each { userName, realName ->
            def user = new Person(username: userName, realName: realName, password: password, enabled: true).save()
            PersonAuthority.create user, userRole, true
        }
    }
}
应用程序启动时加载这个文件,调用init里面的所有的代码。然后,该程序会检查Person是否有记录。如果没有,它将调用createData()方法,创建虚拟用户。
现在让我们来运行应用程序。
grails run-app
一旦加载完毕,将呈现应用程序项目网站链接。默认情况下,它是:
http://localhost:8080/simple-twitter


您将看到两个控制器链接, 名称是: LoginController 和LogoutController。LoginController 用于用户登录,  LogoutController用于注销。


当您点击 LoginController, 您会看到上面的页面. 您如果要成功登录,必须提供正确的用户名和密码。


您可以输入任何用户名,只要是在BootStrap.groovy文件中存在的。默认情况下,所有用户的密码是:“password”,不带引号。


在接下来的教程中,我们将学习如何为用户添加状态消息。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值