`

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

 
阅读更多

本文来自:fair-jm.iteye.com 转截请注明出处

 

本文是按照网上已有的案例来的:

http://blog.csdn.net/laoxue6699/article/details/9722111

 

但网上这个案例是2.0版本 并不适用2.3.2 实际上文中说的安装插件等命令在2.3.2中已经被弃用了

而且很多配置也产生较大的变化

 

具体过程和上面所给网址一样 本人也是刚刚接触grails 具体学习的资源就是官网的manual和stackoverflow中有关grails的问题等

如有错误 欢迎纠正

 

 

在安装好grails后 运行以下指令新建一个项目:

grails create-app simple-twitter  //在当前目录新建一个项目
cd simple-twitter //进入项目根目录

 

安装插件的方式已经改变了

现在的方式在grails-app/conf/buildConfig.groovy

在plugins下加入

compile ":spring-security-core:2.0-RC2"//不知道定位的先用 grails install-plugin spring-security-core 这个命令本身不会安装插件了 但会有相关安装插件的提示

 这样还不行 还得加入一个spring的repo

在repositories下加入:

mavenRepo 'http://repo.spring.io/milestone'

 好 先展现一下buildConfig.groovy的样子:

grails.servlet.version = "3.0" // Change depending on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.work.dir = "target/work"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"

/*
grails.project.fork = [
    // configure settings for compilation JVM, note that if you alter the Groovy version forked compilation is required
    //  compile: [maxMemory: 256, minMemory: 64, debug: false, maxPerm: 256, daemon:true],

    // configure settings for the test-app JVM, uses the daemon by default
    test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
    // configure settings for the run-app JVM
    run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
    // configure settings for the run-war JVM
    war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
    // configure settings for the Console UI JVM
    console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]
*/

grails.project.dependency.resolver = "maven" // or ivy
grails.project.dependency.resolution = {
    // inherit Grails' default dependencies
    inherits("global") {
        // specify dependency exclusions here; for example, uncomment this to disable ehcache:
        // excludes 'ehcache'
    }
    log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
    checksums true // Whether to verify checksums on resolve
    legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility

    repositories {
        inherits true // Whether to inherit repository definitions from plugins

        grailsPlugins()
        grailsHome()
        mavenLocal()
        grailsCentral()
        mavenCentral()
        // uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
        //mavenRepo "http://repository.codehaus.org"
        //mavenRepo "http://download.java.net/maven/2/"
        //mavenRepo "http://repository.jboss.com/maven2/"
		mavenRepo 'http://repo.spring.io/milestone' //spring的仓库
    }

    dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
        // runtime 'mysql:mysql-connector-java:5.1.24'
		runtime 'mysql:mysql-connector-java:5.1.21' //这个是加mysql的依赖要用的话先把这个jar包放在根目录下的lib目录下
    }

    plugins {
        // plugins for the build system only
        build ":tomcat:7.0.42"

        // plugins for the compile step
        compile ":scaffolding:2.0.1"
        compile ':cache:1.1.1'
		compile ":spring-security-core:2.0-RC2" //这个是新增的

        // plugins needed at runtime but not for compilation
        runtime ":hibernate:3.6.10.3" // or ":hibernate4:4.1.11.2"
        runtime ":database-migration:1.3.8"
        runtime ":jquery:1.10.2"
        runtime ":resources:1.2.1"
        // Uncomment these (or add new ones) to enable additional resources capabilities
        //runtime ":zipped-resources:1.0.1"
        //runtime ":cached-resources:1.1"
        //runtime ":yui-minify-resources:0.1.5"
    }
}

 

接下来执行下

 grails compile

 

然后就可以用s2-quickstart等命令了

 

grails s2-quickstart org.grails.twitter(这个是包名) Person Authority

 这样会在domain下产生三个领域对象

 

与链接中的文章不同 要在bootstrap中进行插入操作 必须使用事务:

import org.cc.twitter.*

class BootStrap {

    def init = { servletContext ->
         if (!Person.count()) {
            createData()
        }
    }
    def destroy = {
    }
	
   private void createData() {
   
    def userRole=null
    Authority.withTransaction{
      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'
	Person.withTransaction{
	[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
	}
	}
}
}

 

接下去用grails run-app 或者run-app(交互模式下)

没问题的话 就可以看到界面了

但此时的logout那个链接点进去是405(不能使用get方法来logout) 这是spring-security-core 2中默认的

要改变规则在 config.groovy中加入:

grails.plugin.springsecurity.logout.postOnly = false

 然后stop-app再run-app 如果还不行 那么把交互模式关掉 再进去 我在这个步骤里 试了下用clean 结果再run-app的时候出现一堆错误 但是重新打开再进去 就完全没有问题了...

 

 

 

附:

1、如果执行run-app的操作 出现fork错误

那么把BuildConfig.groovy中的grails.project.fork给注释掉

 

2、在datasource.groovy中增加mysql支持:

dataSource {
    pooled = true
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
}

dataSource_mysql { 
	dialect = org.hibernate.dialect.MySQLDialect 
	driverClassName = 'com.mysql.jdbc.Driver' 
	username = 'root' 
	password = '' 
	url = 'jdbc:mysql://localhost:3306/grails_twitter' 
	dbCreate = 'update'  //只更新 不创建 
} 

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
//    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
}

// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
            properties {
               maxActive = -1
               minEvictableIdleTimeMillis=1800000
               timeBetweenEvictionRunsMillis=1800000
               numTestsPerEvictionRun=3
               testOnBorrow=true
               testWhileIdle=true
               testOnReturn=false
               validationQuery="SELECT 1"
               jdbcInterceptors="ConnectionState"
            }
        }
    }
}

 要是用mysql

只需在domain中加入

static mapping={
  datasource 'mysql'  
  //mapping还支持修改表明 修改列名等操作
  /*
  table '表名'
    columns {
      属性名 column:'列名'
    }
  */
}

 

0
0
分享到:
评论

相关推荐

    Grails+快速开发+Web+应用程序.pdf

    本教程介绍 Grails,这是一个搭建在动态语言 Groovy 之上的开源 MVC 快速 Web 开发框架。使用 Grails 可以提高 Web 开发的效率,降低 Web 开发的复杂度。本文 从 Grails 自动生成代码入手,以示例为中心逐步增加深度...

    使用_Grails_快速开发_Web_应用程序

    使用_Grails_快速开发_Web_应用程序

    Eclipse下搭建Grails项目

    Grails项目的应用越来越多,而对于初学者来说,在Eclipse下搭建Grails项目是一个难题,这个文档将教会你如何搭建Grails项目,希望对你有所帮助。

    Grails权威指南

    第1章 寻找grails之旅  1.1 java的困惑  1.2 webc2.0时代  1.3 java的力量  1.4 什么是grails  1.4.1 与java集成  1.4.2 简单而强大  1.4.3 吸取的经验教训  1.5 使用grails的原因 ...

    第一个grails程序

    自己动手一步步写出来的简单登录验证程序,代码比较简单,可以随便看看。

    在Eclipse里使用Grails类创建工具.pdf

    我们知道,在 Grails 项目中使用Grails类创建工具如“grails create-domain-class”等,能 帮我们创建domain、service 和controller等类及它们的测试类及其他。因此,我们在Grails 项目 中常常是使用这些工具帮我们...

    Grails1.1中文文档

    一个内嵌的 Jetty 容器被配置用来快速重载应用 Spring 容器内建的依赖注入技术 基于 Spring 的 MessageSource 核心概念的国际化 (i18n) 支持 基于 Spring 的抽象事务概念的事务服务层 所有这些都非常易于使用,...

    Grails中文参考手册

    Grails 中文 参考手册

    使用GORM构建Spring Boot应用程序 Grails指南_ Grails框架.pdf

    使用GORM构建Spring Boot应用程序 Grails指南_ Grails框架.pdf

    使用 Grails 快速开发 Web 应用程序

    Grails入门好资料

    grails中文参考手册

    grails中文参考手册 学习资料 groovy

    grails 1.0.4

    Grails尽量为更多现有的Java项目创建一个全面的框架(不仅局限于视图处理),这和当前一些Java框架提供给用户的一种AnemicAPI形成了明显的对比。Grails的出现并不是偶然的,而是随着Web应用的日趋复杂及Web2.0和Ajax...

    grails-petclinic, Grails的介绍性示例应用程序.zip

    grails-petclinic, Grails的介绍性示例应用程序 Petclinic示例应用程序这是Grails标准的介绍性示例应用程序。 要开始使用它,只需克隆存储库,然后从本地副本运行中进行操作: ./gradlew run 在unix系统上,或者 ...

    Grails 中文参考手册

    2.2 创建一个Grails应用 2.3 Hello World示例 2.4 使用IDE 2.5 规约配置 2.6 运行Grails应用 2.7 测试Grails应用 2.8 部署Grails应用 2.9 所支持的Java EE容器 2.10 创建工件 2.11 生成Grails应用 3. 配置 3.1 基本...

    grails 中文第二版

    grails 中文第二版

    Grails1.1中文文档(CHM)

    Grails1.1最新 中文 文档 当今的Java Web开发技术...Grails建立在这些概念之上,它极大地降低了在Java平台上建立Web应用的复杂性。与那些框架不同的是,Grails是构建在现有的像Spring、Hibernate这样的Java技术之上。

    grails-test-app:使用grails和gradle构建简单的Web应用程序

    使用Robotframework( )编写一个测试用例,以在Google上搜索“ Ruckus Wireless”,并检查是否找到任何结果。 导出RESTful API端点以运行测试用例。 使用Gradle&Docker将应用程序打包到容器中。 向我发送...

    Groovy轻松入门——Grails实战基础篇

    学Groovy,Grails 与学Java一样,在实战之前需要搭建开发环境,您可以在 Groovy轻松入门--搭建Groovy开发环境 学习到如何搭建Groovy环境,之后我会讲一下如何搭建Grails环境,然后手把手地写个Demo程序告终,我还会...

    Grails Grails Grails

    Grails Grails Grails Grails Grails

    Grails 1.1 中文手册 chm

    Grails 最新的 v1.1版的中文文档,chm格式,Grails是一套快速开发Web应用的开源框架,基于Groovy编程语言,并构建于Spring、Hibernate和其它标准Java框架之上,能为大家带来超高效率的一站式框架。

Global site tag (gtag.js) - Google Analytics