Composition on Grails - First Application

Creating your First Application

The Basics

  1. If you have an open command prompt from running runMeFirst.bat while setting up Composition on Grails on NW CE, then you can skip steps 2 & 3 and continue to use this command prompt for the rest of the tutorial.
  2. Go to the Grails folder C:\workspace\grails1.0.
  3. Run RunMeFirst.bat. This should open a command prompt in C:\workspace\grails1.0\cog-apps folder. Alternatively, you can use "External Tools" in Eclipse for running grails commands instead of using the command prompt.
    Don't close the command prompt as you have to use it for entering commands throughout the tutorial.
  4. Create a Composition on Grails application.
    grails create-app MyFirstApplication
    
  5. Create a domain class.
    cd MyFirstApplication
    
    grails create-domain-class Book
    
    grails create-domain-class Author
    
  6. Edit the domain class "Book" that you created. You'll find it in grails workspace\cog-apps\MyFirstApplication\grails-app\domain. (This is in the file system and not on cmd, you can use any text editor for the same.)
    class Book{
      String title
      Double price
      Author author
    }
    
  7. Now paste the following block of code into the Author domain class.
    class Author{
      String name
      String profession
      static hasMany = [ books : Book ]
    
      String toString(){
         if(name!=null)
    	return name;
         else
    	return "Author: "+id;
      }
    }
    
  8. Go back to command prompt and generate the Web Dynpro controllers and views.
    grails cog-generate-all Book
    
    grails cog-generate-all Author
    
  9. Generate dev.html. This file is used at design time to see changes at the page reload.
    grails cog-generate-html
    
  10. Click on dev.html (in workspace\cog-apps\MyFirstApplication) to see your first Composition on Grails application.
    Avoid instantiating portal specific objects such as "logged in user" as member variables. If not properly declared, they will cause failures in development mode (dev.html).

Configuring your Application

Now that you have a running application, you can modify it to make it more interesting. If you create something really cool and you're willing to share, let us know.

  1. You can change any of the generated controllers and views found in C:\workspace\grails1.0\cog-apps\yourApplication\grails-app\wdcontrollers according to your needs.
  2. You can add non-UI business logic to the services folder found in grails-app.

FAQ for creating CoGs Application

When I click on dev.html it gives me a "Page cannot be displayed" error.

Go to your Composition on Grails application directory, e.g. C:\workspace\grails1.0\cog-apps\MyFirstApplication. You should find a cogConfig.properties file here. Right click and open this with a text editor. Make sure that the http protocol port number listed here matches with the http port number in CE. To check this, go to the SAP Management Console, CE and under the J2EE server, go to Access Points and make sure that the port number for HTTP protocol matches what is specified in cogConfig.properties. If not, update cogConfig.properties with this port number and run the cog-generate-html script again.

The changes I made to my domain class don't seem to be reflected in the UI in my application.

In your application, right click on any controller and open in new window. Then in the address bar of your browser, at the end of the present URL type &reload=true . This would force a compilation of all the groovy files in your application.

When I do reload=true it cleans up my database.

Grails by default is set to use HyperSonic database which is a in-memory database. You can set grails to use MySQL if you wish to keep your database intact.

I'm running in dev mode (using dev.html) and the CE server keeps going down.

Most likely there is a syntax error in the code which is not deployed to the j2ee engine. Workaround: deploy your application, this will throw the correct error or debug. Once you fix the errors, you shouldn't have the problem anymore.

This is not my first application, is there something different that I need to do?

If its your second or later application, you should launch a command prompt using RunMeFirst.bat. Please note that using setUp.bat would regenerate grails.sda before launching the command prompt. This isn't required unless you have new additional libraries added that are not yet deployed.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.