【问题标题】:Insert into Database Groovy Grails插入数据库 Groovy Grails
【发布时间】:2016-01-08 04:07:18
【问题描述】:

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

【问题讨论】:

  • 请添加您的User 域类。 Grails 为您提供了 GORM,它使与持久性相关的东西变得容易。无需使用 SQL 插入。你使用什么 Grails 版本?
  • 我的 Grails 版本是 2.4.4 @saw303 先生,我如何使用 GORM 插入数据库?

标签: grails grails-2.4


【解决方案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')}

【讨论】:

  • 感谢您的回答,但是当我单击 register.gsp 上的“注册”按钮时,它会将我重定向到“登录”页面并给我一个错误,上面写着“抱歉,${params.userName} , 请再试一次。”我不明白为什么注册按钮会将我重定向到登录页面并尝试登录?
  • 你在使用 Grails Spring Security 插件吗?
  • 我的插件插件 { 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" // 或 ":hibernate:3.6.10.18" runtime ":database-migration:1.4.0" runtime": jquery:1.11.1" }
  • 你的 UserController 的 index 动作默认是登录页面。要查看实际效果,请创建一个新视图并改为渲染该视图。
猜你喜欢
  • 2015-11-02
  • 1970-01-01
  • 2012-02-08
  • 2011-09-07
  • 2011-01-07
  • 2011-01-20
  • 2015-03-14
  • 1970-01-01
相关资源
最近更新 更多