Below is code which save user object and print the list of users in the console.
index.gsp
<g:form action="signUp" controller="User">
E-mail Id :: <g:textField name="emailId" type="text"/><br>
Mobile Number :: <g:textField name="mobileNumber" type="text" /> <br>
<g:submitButton name="Sign up"/>
</g:form>
User.groovy
class User {
private String emailId;
private int mobileNumber;
}
UserController.groovy
class UserController {
def signUp() {
def user = new User(params);
user.save();
List<User> userList = User.list();
print "Size of list::"+userList.size();
for(User user1 : userList){
print "User Id::"+user1.id;
print "Email id::"+user1.emailId;
print "Mobile Number::"+user1.mobileNumber;
}
}
}
For now I haven't added any service layer. Below are my inputs and respective outputs.
Input-1
Email - id : test@test.com
Mobile number : 1234
Output-1 :
....Size of list::1
User Id::1
Email id::test@test.com
Mobile Number::1234
Which is perfect, now I am inserting one more user.
Input-2
Email - id : sample@sample.com
Mobile number : 5678
Output-2
Size of list::2
User Id::1
Email id::null
Mobile Number::0
User Id::2
Email id::sample@sample.com
Mobile Number::5678
It will replace all previous record with either null or 0(if int).
you shall never define the domain properties as private fields
!