Insert into Database Groovy Grails -


i'm new developer groovy grails , have table named user under database test. succeed login database using grails couldn't succeed register new user.

here 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 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 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 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 , had error

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

but couldn't understand tried say

here 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" // 1 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 assure every information need thank you

my grails version 2.4.4

as @saw303 mentioned, gorm provides you're looking for. there's no need write sql statements.

workflow

i'm assuming workflow need this:

  1. the register action renders register.gsp, registration form.
  2. when registration form submitted registration action handles request. if goes well, user set in session scope , browser redirected somewhere, such home page. otherwise, browser redirected register action , validation errors displayed in registration form.

here's how create such workflow.

registration action

begin making number of changes registration action.

import grails.transaction.transactional  class usercontroller {      // note: making method transactional avoids having flush saves.     @transactional     def registration() {         def user = new user(params).save()          if(user.haserrors()) {             /*              * on failure, redirect registration form,              * , pass user instance gsp page in              * flash scope validation errors can rendered.              */             flash.userinstance = user             redirect action: 'register'         } else {             /* on success, place user instance in session scope,              * , redirect home page.              */             session.user = user              redirect uri: '/'          }      } } 
  1. i added transactional annotation flushing of gorm saves handled automatically.
  2. registration method instead of closure. it's new way.

register.gsp

to render validation errors in registration form, change haserrors() calls bean: userinstance model: flash?.userinstance example, fullname property, ${haserrors(model: flash?.userinstance, field: 'fullname', 'error')}


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -