0

I'm a new developer for groovy grails and I have a table named user under the database test. I succeed to login to that database by using grails but I couldn't succeed to register the new user.

Here is my 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>

here is my 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")
       }

    }

After this method , I had this error

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

But I couldn't understand what it tried to say

here is my 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"
        }
    }
}

My domain class User.groovy

package test

class User {

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


static constraints = {

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

}

Plug-in's

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"

    }

I can immediately assure every information you need Thank you

My Grails version is 2.4.4

Burak
  • 133
  • 2
  • 12
  • Please add your `User` domain class. Grails provides you GORM that makes the persistence related stuff easy as pie. There is no need to work with SQL inserts. What Grails version do you use? – saw303 Oct 10 '15 at 09:18
  • My Grails version is 2.4.4 @saw303 how can I use GORM to insert into database sir ? – Burak Oct 10 '15 at 13:16

1 Answers1

1

As @saw303 mentioned, GORM provides what you're looking for. So there's no need to write SQL statements.

Workflow

I'm assuming the workflow you need is something like this:

  1. The register action renders register.gsp, the registration form.
  2. When the registration form is submitted the registration action handles the request. If all goes well, the user is set in the session scope and the browser is redirected somewhere, such as the home page. Otherwise, the browser is redirected to the register action and the validation errors are displayed in the registration form.

Here's how to create such a workflow.

Registration action

Begin by making a number of changes to the registration action.

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. I added the Transactional annotation so the flushing of GORM saves are handled automatically.
  2. registration is now a method instead of a Closure. It's the new way.

register.gsp

To render the validation errors in the registration form, change the hasErrors() calls from bean: userInstance to model: flash?.userInstance For example, for the fullName property, do ${hasErrors(model: flash?.userInstance, field: 'fullName', 'error')}

Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
  • thanks for your answer but when I click on 'register' button on register.gsp, it redirects me to 'login' page and give me an error which say "Sorry, ${params.userName}, Please try again." What I don't understand is why register button redirects me to login page and tries to login? – Burak Oct 10 '15 at 15:48
  • Are you using the Grails Spring Security plugin? – Emmanuel Rosa Oct 10 '15 at 16:46
  • My **plug-in's** 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" } – Burak Oct 10 '15 at 21:20
  • The index action of your UserController defaults to the login page. To see this in action create a new view and render that view instead. – Neoryder Oct 13 '15 at 08:25