Reputation: 155
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
Upvotes: 0
Views: 3451
Reputation: 9885
As @saw303 mentioned, GORM provides what you're looking for. So there's no need to write SQL statements.
I'm assuming the workflow you need is something like this:
register
action renders register.gsp, the registration form.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.
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: '/'
}
}
}
registration
is now a method instead of a Closure. It's the new way.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')}
Upvotes: 1