Sunday, 9 September 2012


HI THIS TOUTORIAL WILL HELP FULL FOR STARTUP IN GRAILS CREATED BY NAVEENRAJU
Parts




Part3:

1.creating a simple domain class which will perform(save,show,edit,delete) operations

  with scaffold in controller


Step1: grails create-app UsingScaffolding

 Step2:     cd  UsingScaffolding

Step3:grails  create-domain-class Demo1

Step4: open folloing path and type this code

         c:/ UsingScaffolding/grails-app/domain/usingScaffolding/Demo1.groovy

   package usingscaffolding
class Demo1 {
String   name
Integer  age
String   sex
String  country
}

Step5: go to cmd promt and create one controoler

           grails  create-controller Demo1

Step6: Open folloing path and type code like this

                     c:/ UsingScaffolding/grails-app/controllers/usingScaffolding/Demo1Controller.groovy
          package usingscaffolding
class Demo1Controller {
    def scaffold=Demo1
}

      step7:run this application

                grails run-app

     step8:open any Browser And type this url

 http://localhost:8080/UsingScaffolding


  step9: then click this link 

              usingscaffolding.Demo1Controller
               


step10: Then list page is opened  Afterwords click  NewDemo1 link

·                      

step11: fill the form and click create link



step12:After filling form then click  on create link show page will Open if u whant to edit click edit on link




step13:then edit  the page  after editing click update


            

step14:if u whant  to delete press on delete link




Note : From the above domain class will perform (save,edit,show,delete) operations
            because  in controller we mention  "def scaffold= domainclassName " hence in back ground
            it will create  5views (form.gsp,create.gsp,edit.gsp,list.gsp,show.gsp)  And
            it will create  in controller  7 actions (list,create,save,edit,show,update,delete)
            in part 6 we will see how we generate an aplication with all features with out scaffold


2)How to do Search After doing all operations  i am searching  with name

   in o/p i will reteive name and country with respect to searched name

Step1: i am doing modifications on above controller

step2:     c:/ UsingScaffolding/grails-app/controllers/usingScaffolding/Demo1Controller.groovy

              and recode like this
package usingscaffolding
class Demo1Controller {
def scaffold=Demo1
def search = {
if (request.method == 'POST')
{
def quary1= Demo1.findAllByNameLike('%' + params.name + '%')
[quary1:quary1]
}}}


step3: with respect to controller action search  create view (search.gsp) in folloing  path

            c:/ UsingScaffolding/grails-app/views/demo1/search.gsp
        code like this
    <html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/>
<meta name="layout" content="main" />
<title>Search for The Names</title>
</head>
<body>
<div class="body">
<h1>Search for for The Names</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<g:form action="search" method="post" >
<div class="dialog">
<table>
<tr class='prop'>
<td valign='top' class='name'>
<label for='city'>Name:</label>
</td>
<td valign='top' class='value'>
<input type="text" maxlength='30' name='name'>
</input>
</td>
</tr>
</table>
</div>
<div class="buttons">
<input type="submit" value="Search"
class="formbutton">
</input>
</div>
</g:form>
<div class="dialog">
<table>
<tbody>
<g:each in="${quary1}">
<tr class="prop">
      <td valign="top" class="name">  Names</td>
       <td valign="top" class="value"> ${it.name} </td>
 </tr>
<tr class="prop">
      <td valign="top" class="name"> Country</td>
       <td valign="top" class="value"> ${it.country} </td>
 </tr>
</g:each>
</tbody>
</table>                             
</div>
</div>
</body>
</html>

Note : <meta name="layout" content="main" />
            it will give grails Header  and u can use those classes

step4: go to command prompt and run the server

          grails run-app

   http://localhost:8080/UsingScaffolding/

step6:Enter the form up to 5 fields

      

step7:After entering fields in addressbar  type this url afterwords type name and seach


Step9:then  final output will be like this


            


 

  3) from above examle validations are there how to remove validations in DomainClass











step1:i am doing modifications on above domain class---->class  Demo1

step2: open folloing path 

        c:/ UsingScaffolding/grails-app/domain/usingScaffolding/Demo1.groovy
        re code like this
package usingscaffolding

class Demo1 {
String   name
Integer  age
String   sex
String  country
String  address
static constraints = {
age(nullable:true,blank:true)//in this nullabule,blank is acepted
sex(nullable:true,blank:true)//in this nullabule,blank is acepted
name(nullable:false,blank:false)//in this nullabule,blank is not acepted
country(nullable:false,blank:false)//in this nullabule,blank is not acepted
address(nullable:true,blank:true,size:3..500)/*in this nullabule,blank is acepted
                                               and textbox size will be 3to500*/
 }
}

step3: then the output will be like this









4.How To Pick Random list from above list it generated

step1: know i am doing modifications  on  Controller

step2:Open Folloing link

          c:/ UsingScaffolding/grails-app/controllers/usingScaffolding/Demo1Controller.groovy
  re code like this
 package usingscaffolding

class Demo1Controller {

    def scaffold=Demo1





def search = {
if (request.method == 'POST')
{
def quary1= Demo1.findAllByNameLike('%' + params.name + '%')
[quary1:quary1]
//render  model:[ raceList:Demo1.findAllByNameLike('%' + params.name + '%') ]
}
}

def random = {
def list = Demo1.list()
def raja
if (list.size() > 0) {
def randomPic = new Random().nextInt(list.size())
raja = list[randomPic]
} else {
 raja = new Demo1(name: "Raja",
age: "23",sex:"male",country:"India",address:"Hyderabad")
}
[ raja : raja]
}
}
         

step3:  with respect to controller action search  create view (search.gsp) in folloing  path

            c:/ UsingScaffolding/grails-app/views/demo1/random.gsp
     code like this
<html>
<head>
<title>Random</title>

</head>
<body>
<g:each in="${raja}">
<div id="raja">
<a>${it.name}</a>
<b>${it.country}</b>
<c>${it.age}</c>
<d>${it.sex}</d>
<e>${it.address}</e>
</div>
</g:each>
</body>
</html>

step4:run the application

           grails run-app
browse to above link

step5: Fill the form up to 5 fields


step6:type folloing url in address bar and refersh it for 10 times then it will pick random list details 






5)before  filling the form i am placing 1 field by using Bottstrap

Step1:from the above randompick example starting list is empty

         after filling the form only we can do random operation
        with out filling form  i am adding 1 row in list by this example after that u can fill list as usal
       but first row i am adding

step2: open folloing link  and create NaveenBootStarp.groovy

           c:/ UsingScaffolding/grails-app/conf/ NaveenBootStarp.groovy
           code like this in NaveenBootStrap.groovy
package usingscaffolding
class NaveenBootStrap {
def init = { servletContext ->
// We can Create some Test Data Here that are inserted in list
new Demo1(name: "Raja",age: "23",sex:"male",country:"India",address:"Hyderabad").save()

}
def destroy = {
}
}

step3:after that run this application

               grails run-app

step4:click on usingscaffolding.Demo1Controller

hence with out  filling form we can insert data to list by crating  ur ownBootStrap
in conf  folder









       
         
        





             


No comments:

Post a Comment