插入数据库Groovy Grails

时间:2015-10-10 07:06:03

标签: grails grails-2.4

我是groovy grails的新开发人员,我在数据库测试下有一个名为user的表。 我成功通过使用grails登录到该数据库,但我无法注册新用户。

这是我的 register.gsp

<g:form controller="user" action="registration">
                <fieldset class="form">
                    <div class="fieldcontation ${hasErrors(bean: userInstance, field: 'fullName', 'error')}">
                        <label for="fullName">
                            <g:message code="endUser.fullName.label" default="Full Name" />
                        </label>
                            <g:textField name="fullName" value="${userInstance?.fullName}"/>
                    </div>
                    <div class="fieldcontation ${hasErrors(bean: userInstance, field: 'userName', 'error')}">
                        <label for="userName">
                            <g:message code="endUser.userName.label" default="User Name" />
                        </label>
                            <g:textField name="userName" value="${userInstance?.userName}"/>
                    </div>
                    <div class="fieldcontain ${hasErrors(bean: userInstance, field: 'password', 'error')} ">
                        <label for="password">
                            <g:message code="endUser.password.label" default="Password"/>
                        </label>
                        <g:field type="password" name="password" value="${userInstance?.password}"/>
                    </div>

                </fieldset>
                <fieldset class="buttons">
                    <g:submitButton name="register" class="save" value="Register" />
                </fieldset>
            </g:form>

这是我的 UserController.groovy

package test

import java.sql.SQLClientInfoException;

import javax.activation.DataSource;

import grails.converters.JSON

class UserController {

def index() { 

    redirect(action:"login")

}

def register = {}

def login = {}

def registration = {

     def b = new User(fullName:params.fullName, userName:params.userName, password:params.password)
        b.save()            
        render (b as JSON)

}

def authenticate = {
   def user = User.findByUserNameAndPassword(params.userName, params.password)
   if (user){
       def userMap = [:]
       userMap.put("login", "true")
       userMap.put("name", user.fullName)
       userMap.put("password", user.password)   
       render (userMap as JSON)

   }else{
       flash.message = "Sorry, ${params.userName}, Please try again."
       redirect(action:"login")
   }


 }


    def logout = {
           flash.message = "Goodbye ${session.user.fullName}"
           session.user = null
           redirect(action:"login")
       }

    }

此方法之后,我遇到了此错误

http://postimg.org/image/gbitzsokp/

但我无法理解它试图说的是什么

这是我的 DataSource.groovy

dataSource {
    pooled = true
    jmxExport = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "admin"
    password = "admin"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider'
}

// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:mysql://localhost:3306/test"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:mysql://localhost:3306/test"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:mysql://localhost:3306/test"
        }
    }
}

我的域类 User.groovy

包裹测试

class User {

String userName
String password
String fullName
String toString(){
    "${fullName}"
}


static constraints = {

    fullName()
    userName(unique: true)
    //password(password=true)
}

}

插入式的

plugins {
        build ":tomcat:7.0.55"


        compile ":scaffolding:2.1.2"
        compile ':cache:1.1.8'
        compile ":asset-pipeline:1.9.9"
        compile ":simpledb:0.5"

        runtime ":hibernate4:4.3.6.1" // or ":hibernate:3.6.10.18"
        runtime ":database-migration:1.4.0"
        runtime ":jquery:1.11.1"

    }

我可以立即确保您需要的所有信息 谢谢

我的Grails版本为 2.4.4

1 个答案:

答案 0 :(得分:1)

正如@ saw303所提到的,GORM提供了您正在寻找的东西。所以不需要编写SQL语句。

工作流

我假设您需要的工作流程是这样的:

  1. register操作呈现 register.gsp ,即注册表单。
  2. 提交注册表单后,registration操作会处理该请求。如果一切顺利,则将用户设置在会话范围内,并将浏览器重定向到某个位置,例如主页。否则,浏览器将重定向到register操作,验证错误将显示在注册表单中。
  3. 以下是如何创建此类工作流程。

    注册行动

    首先对registration操作进行一些更改。

    import grails.transaction.Transactional
    
    class UserController {
    
        // NOTE: Making the method Transactional avoids having to flush saves.
        @Transactional
        def registration() {
            def user = new User(params).save()
    
            if(user.hasErrors()) {
                /*
                 * On failure, redirect back to registration form,
                 * and pass the User instance to the GSP page in the
                 * flash scope so that validation errors can be rendered.
                 */
                flash.userInstance = user
                redirect action: 'register'
            } else {
                /* On success, place User instance in session scope,
                 * and redirect to home page.
                 */
                session.user = user 
                redirect uri: '/' 
            } 
        }
    }
    
    1. 我添加了Transactional注释,以便自动处理GORM保存的刷新。
    2. registration现在是一种方法,而不是Closure。这是新的方式。
    3. register.gsp

      要在注册表单中呈现验证错误,请将hasErrors()来电从bean: userInstance更改为model: flash?.userInstance例如,对于 fullName 属性,请执行{ {1}}