mrice00 2013-11-24
本文是按照网上已有的案例来的:
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:'列名' } */ }