Monday, May 10, 2010

Grails remote jQuery form validation

<script type="text/javascript">

$(function (){

    $("#form").validate({

        rules: {

            'address.postalCode': {

                required: true

            },

        },

        messages: {

            'address.postalCode': {

                required: "Please enter a postal code."

            }

        }

    });

});

</script>

<g:form method="post" action="register" name="form">

    <input type="text" name="address.postalCode" id="postalCode" />

    <input type="submit" value="Proceed" />

</g:form>

Grails Return JSON

Domain Class:

class MyDomain {


String name


String address


String gender

}

Controller Class :

class MyController {


def someaction =
{


List myDomains = MyDomain.findAllByGender(params.gender)


<em>/*Let say 5 objects are retrieved*/</em>

render myDomains as JSON


}

}

The above code in action will render a JSON string in the following format:

[
{
"name" : "John",
"address" : "New York",
"gender" : "Male"
},
{
"name" : "Rob",
"address" : "Indonasia",
"gender" : "Male"
},
{
"name" : "Shayam",
"address" : "New Delhi",
"gender" : "Male"
},
{
"name" : "Chang",
"address" : "Thailand",
"gender" : "Male"
},
{
"name" : "Ali",
"address" : "London",
"gender" : "Male"
}
]

following script would be writtent on the client Side (in the GSP):

<script>

$.document.ready(function(){

$('#someid').click(function(){

<strong>$.getJSON</strong>("${createLink(controller:'my',action:'someaction')}",{gender:'Male', ajax:
'true'},
function(myDomains){

var myHTMLString =
''

for(var i =
0
; i < myDomains.length
; i++)

{

myHTMLString = myHTMLString +
'<tr><td>'
+ myDomains[i].name
+
'</td>'

myHTMLString = myHTMLString +
'<tr><td>'
+ myDomains[i].address
+
'</td>'

myHTMLString = myHTMLString +
'<tr><td>'
+ myDomains[i].gender
+
'</td></tr>'

}

$('table#mytable').html(myHTMLString)

})

})

})

</script>


 

The HTML code would be like this :

<div id="someid">
Click for JSON Response
</div>
<table id="mytable">
<!–empty table –>
</table>

When you click on the displayed text the html page would become like following:

<div id="someid">
Click for JSON Response
</div>
<table id="mytable">
<tr>
<td>John</td>
<td>New York</td>
<td>Male</td>
</tr>
<tr>
<td>Rob</td>
<td>Indonasia</td>
<td>Male</td>
</tr>
<tr>
<td>Shayam</td>
<td>New Delhi</td>
<td>Male</td>
</tr>
<tr>
<td>Chang</td>
<td>Thailand</td>
<td>Male</td>
</tr>
<tr>
<td>Ali</td>
<td>London</td>
<td>Male</td>
</tr>
</table>

any kind of suggestions and comments are welcome……

Groovy List<Integer>

List<Integer> list = [5,6,7,8]

list.each({line -> println line})

list.each({println it})

Basic Groovy Lang…

println '######################## List ##########################\n'

List<Integer> list = [1,2,3,4,5,6,7,7,6,5,6,7,5,6,5]

println 'value list[0] is '+list[0]

println 'value list[3] is '+list[3]

println 'value list[8] is '+list[8]

println 'size = '+list.size()


 

println '######################## MAP ##########################\n'

Map map = [:]

def map2 = ["Jim":"Knopf", "Thomas":"Edison"]

println map2["Jim"]

map2["Test"] = "Tester"

println map2["Test"]


 

println '######################## For ##########################\n'

for (i in 0..9) {

println ("Hello $i" )

}


 

println '######################## Defines a string with special signs ##########################\n'

// Defines a string with special signs

def text = "John Jimbo jingeled happily ever after"


 

// Every word must be followed by a nonword character

// Match

if (text==~/(\w*\W+)*/){

println "Match was successful"

} else {

println "Match was not successful"

}

// Every word must be followed by a nonword character

// Find

if (text=~/(\w*\W+)*/){

println "Find was successful"

} else {

println "Find was not successful"

}


 

if (text==~/^J.*/ ){

println "There was a match"

} else {

println "No match found"

}

def newText = text.replaceAll(/\w+/, "hubba")

println newText

Tuesday, May 4, 2010

Crate Grails

#1

C:\>grails create-app MyApp

Welcome to Grails 1.2.1 - http://grails.org/

Licensed under Apache Standard License 2.0

Grails home is set to: C:\grails-1.2.1


 

จากนั้น


 

#2

grails tomcat

Found events script in plugin tomcat

Created Grails Application at C:\/MyApp

C:\>cd MyApp


 

#3

C:\MyApp>grails run-app

Welcome to Grails 1.2.1 - http://grails.org/

Licensed under Apache Standard License 2.0

Grails home is set to: C:\grails-1.2.1


 


 

#4 ทดสอบ

http://localhost:8080/MyApp