grails - 插入数据库Groovy Grails

标签 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

包装测试

类用户{
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

最佳答案

正如@ saw303所提到的,GORM提供了您所需要的。因此,无需编写SQL语句。

工作流程

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

  • register操作将呈现register.gsp(注册表格)。
  • 提交注册表后,registration操作将处理请求。如果一切顺利,则将用户设置在 session 范围内,并将浏览器重定向到某处,例如主页。否则,浏览器将重定向到register操作,并且验证错误将显示在注册表单中。

  • 这是创建此类工作流程的方法。

    注册 Action

    首先对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: '/' 
            } 
        }
    }
    
  • 我添加了Transactional批注,以便自动处理GORM保存的刷新。
  • registration现在是一种方法,而不是Closure。这是新方法。

  • register.gsp

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

    关于grails - 插入数据库Groovy Grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33051300/

    相关文章:

    ruby-on-rails - 编程语言是 "on rails"是什么意思?

    mysql - 使用 Grails 动态脚手架时出现 StaleObjectStateException

    hibernate - Grails 域类事务

    unit-testing - Grails 2.4.3 单元测试数量不正确

    mysql - Grails + BIRT 报告 - 将月份和年份作为日期传递到 BIRT 报告

    grails - 保存大型域对象树的提示

    grails - Grails 2.4.4-如何在 war 阶段存储配置变量并在Tomcat环境中获取它?

    grails - 如何将Grails从Grails 2.4.4升级到Grails 3.3.6?

    sql-server - SQL查询当前不支持返回别名

    java - Spock模拟inputStream导致无限循环