0

我是 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

4

1 回答 1

1

正如@saw303 提到的,GORM 提供了您正在寻找的东西。因此无需编写 SQL 语句。

工作流程

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

  1. register操作会呈现register.gsp,即注册表单。
  2. 提交注册表单后,该registration操作将处理请求。如果一切顺利,用户会被设置在会话范围内,并且浏览器会被重定向到某个地方,例如主页。否则,浏览器将被重定向到该register操作,并且验证错误将显示在注册表单中。

以下是如何创建这样的工作流程。

注册动作

首先对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现在是一个方法而不是一个闭包。这是新的方式。

注册.gsp

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

于 2015-10-10T14:53:08.997 回答