admin « Intelligrape Groovy & Grails Blogs
Subscribe via E-Mail:

admin

http://www.IntelliGrape.com

Posts by admin:

  • How to invoke a GSP tag as a method in Grails GSP

    16 Sep 2008 in Grails& Groovy

    Grails has a lot of gems. One of my favorites (besides GORM) is GSP tags — Grails makes it so easy to create new tags, now there is no reason to write java/groovy code inside GSPs.

    One of the features about GSP tags that I discovered and used today is that it is possible to invoke a GSP tag as a method call and assign the output of the tag to a variable.

    Lets jump to the example:

    <g:def var=”currentDomain” value=”${siteConfig.property(name:’regDomain’)}” />

    In the above example:

    siteConfig : is the namespace of my taglib.

    property : is the name of the tag

    name : is the name of the attribute that I am passing to my tab

    regDomain : is the value of the ‘name’ attribute.

    The output from the tag is stored in currentDomain variable.

    Not sure if something similar is possible with plain JSP tags.

    -Deepak

    • Share/Bookmark
  • How to pass a bean or model to a template

    12 Sep 2008 in Grails& Groovy

    When we want to render a view using a template and the template makes use of a model object, the model object needs to be passed to the template using GSP tags. I am writing this because it took me a while to figure this out. Thanks to the wonderful contributors on the mailing list who helped me with this.

    To render the view to a template from a gsp we need to use this tag in your gsp

    <g:render template=”/templateName”/>

    To send a bean to the template we have to include  an attribute to the tag like this

    <g:render template=”/templateName” bean=”beanName”/>

    (or) we can also use a named variable in the template.For this we have to send a model

    <g:render template="/templateName" model="beanName:${someBeanName}"/>

    In the template we can use this by calling ‘it’  but that’s not my preferred way. The other way is, assign this value to a variable in the template like this.

    <g:set var="variableName" value="${beanName}"/>

    Now we can use the variableName in the template.

    Example:

    <input type="text" id="fieldId" class="${hasErrors(bean: variableName, field:
    'fieldName', 'errors')}" name="fieldName" value="${fieldValue(bean: variableName,
    field: 'fieldName')}"/>

    - Pradeep Garikipati

    • Share/Bookmark
  • Externalize application properties in a Grails app

    03 Sep 2008 in Grails& Groovy

    Externalizing properties of an application has really come a long way, especially if you are using Grails.

    Just want to share as a quick tip how easy it is to externalize the properties in a Grails application.

    This is what all we have to do in-order to get something from a properties file.

    In config.groovy include the properties that you want to be outside your groovy code.

    For Example :

    smtpUserName = "<someid@test.com>"
    hostname = "yourHostName"

    In order to read these properties in your Grails controller:

    def smtpUserName = grailsApplication.config.smtpUserName

    In order to read the properties from Grails Service class:

    def smtpUserName =  ConfigurationHolder.config.smtpUserName
    
    

    Easy, isn’t it?

    -Pradeep Garikipati

    • Share/Bookmark
  • How to customize homepage in Grails app

    17 Aug 2008 in Grails& Groovy

    In this blog I want to share how can we customize home page in a Grails application.

    In grails, the default homepage is the web-app/index.gsp if we explicitly don`t specify any thing; but of-course that is not sufficient for all apps. We need to have a custom landing page instead of the default page provided by grails.

    This can be easily achieved by modifying the URL mappings in UrlMappings.groovy file. 

    "/"
    {
    controller = "yourController"
    action = "yourAction"
    }

    By configuring the URLMappings this way, the home-page of the app will be yourWebApp/yourController/yourAction.

    Hope this helps.

    -Pradeep Garikipati

    • Share/Bookmark
  • Using jQuery and Grails to create chained selects / drop-downs

    In web application development, use of frameworks has become essential . One of those frameworks which help us in making things simpler and life easy is jQuery.

    Why jQuery ?

    • Fully Documented
    • Great Community
    • Tons of plugins
    • Small size(14kb)
    • Everything works in IE 6+,Firefox,Safari 2+,and Opera 9+

    jQquery is a very efficient framework which helps us in developing many things in a much easier way than they would be without using any framework.

    At IntelliGrape we are using Grails and jQuery on a project.

    In this blog, I want to share one of my experiences that I had met in this course of time. A series of mails/questions in Grails mailing list on creating chained selects/drop-downs prompted me to write this blog; since I had already done this.

    Chain select basically means populating the next drop-down on the basis of what you select in the present drop-down.

    In this example, we have two drop-downs – manufacturer and model. We want the model drop-down to be populated on the basis of what the user selects in the manufacturer drop-down, without doing a full page refresh, using an Ajax call.

    Here are my classes, not the whole class but a part of the class

    Vehicle Class

    public  class Vehicle {
      Manufacturer manufacturer
      Model model
    }
    

    The Manufacturer and model classes look like this

    Manufacturer Class

    public class Manufacturer {
      String manufacturerName
      static belongsTo = Vehicle
      static hasMany = [models: Model]
    }
    

    Model Class

    public class Model {
      String modelName
      static belongsTo = Manufacturer
    }
    

    For this the vehicleCreate.gsp should look like this /vehicle/create.gsp

                Home
                Vehicle List
    

    Create Vehicle

    
    ${flash.message}
    <table>
    <tbody>
    <tr>
    <td valign="top"><label for="vehicleName">Vehicle Name:</label></td>
    <td valign="top"></td>
    </tr>
    <tr>
    <td valign="top"><label for="vehicleManufacturer">Manufacturer</label></td>
    <td valign="top"><g:select id="vehicleManufacturerDropDown" optionKey="id"
                                         from="${VehicleManufacturer.list()}"
                                         name="vehicleManufacturerDropDown" value=""/></td>
    </tr>
    <tr>
    <td valign="top"><label for="model">Model</label></td>
    <td valign="top"><g:select id="model" optionKey="id"
                                           from="${VehicleModel.list()}"
                                           name="model" value=""/></td>
    </tr>
    </tbody>
    </table>
    
    $(document).ready(function() {
          $("#vehicleManufacturerDropDown").change(function() {
          $.ajax({
                  url: "/ChainDropDown/vehicle/manufacturerSelected",
                  data: "id=" + this.value,
                  cache: false,
                  success: function(html) {
                  $("#models").html(html);
                  }
                });
             });
           });
    
     Now we will look at how this chained selection works using jQuery.Now what all we are left-out with is
    handling this event in the VehicleController which is as simple as any other controller action.
    Just add manufacturerSelected action in the controller and we are done!
    
    VehicleController
    
    class VehicleController {
      def manufacturerSelected = {
        def manufacturer = VehicleManufacturer.findById(params.id)
        render g.select(optionKey: 'id', from: manufacturer.models, id: 'model', name: "model")
      }
    }
    

    You can download the complete source code for a working sample app with the examples used in this blog. Hope this helps somebody. Pradeep Garikipati

    • Share/Bookmark
  • How to use table-per-subclass inheritance strategy instead of table-per-hierarchy

    15 Jun 2008 in Grails& Groovy

    In this blog I want to share how to use table per sub-class instead of table per hierarchy, which is the default mechanism provided by Grails. I am not sure why Grails chose to use “table-per-hierarchy’. I always find it difficult to understand the table structure produced by “table-per-hierarchy“. Moreover, it makes things difficult for other non-grails application which need to read data from the data database. Another disadvantage is that columns cannot have a "Not Null" constraint applied to them at the db level.

    If you are like me and prefer to use a table-per-subclass inheritance strategy, it can be easily achieved using fantastic Grails GORM DSL.

    Simply include the following code in all your Grails domain classes.

       static mapping = {
       tablePerHierarchy false
    }

    This can be simply specified at the root class level and does-not need to be specified for all sub-classes in the hierarchy.

    Hope this helps.

    -Pradeep Garikipati

    • Share/Bookmark
  • How to make Bootstraping environment specific in a Grails app?

    03 Jun 2008 in Grails& Groovy

    BootStraping is something which is needed in most of the application. One of the frequently asked questions (and a valid requirement as well) is : How do you make Bootstraping happen only in a particular environment.

    This can be done by making use of one of the Grails utility class which allows you to inspect the environment in which the code is executing.

    Example:

    if (GrailsUtil.environment == GrailsApplication.ENV_DEVELOPMENT) {

    // Your bootstraping code here.

    }

    Don’t forget to import the following two classes:

    org.codehaus.groovy.grails.commons.GrailsApplication
    grails.util.GrailsUtil

    Hope this little tips saves you some time.

    - Pradeep Garikipati

    • Share/Bookmark
  • Web Testing using Selenium – fix for error “java.lang.NullPointerException: sessionId should not be null; has this session been started yet?”

    07 May 2008 in Java tools

    I have a big fan of Selenium and have been using Selenium for quite some time for testing of web-applications.

    Recently, I upgraded my machine to Ubuntu Hardy Heron and the next day, I found that all the selenium tests started failling on my machine. On digging through the logs, I found the following information in the Selenium Server logs:

    java.lang.RuntimeException: File was a script file, not a real executable: /usr/bin/firefox-bin

    Before actually spotting this line, I was mis-led by other log messages; but I will not talk about them in this post.

    The reason for the failing tests was that Selenium Server thought that the default executable found in the path (/usr/bin/firefox) was a script file and not an executable application for the browser. I am not sure why Selenium Server checks whether the path to the browser should not be a script.

    The cause of the problem is that because of  Ubuntu Hardy Heron upgrade, my firefox also got upgraded from 2.x to 3.x. The firefox browser executable in 2.x is a binary file; whereas for firefox 3.x, it is a script (.sh) file. As a result of this change, the check done by  Selenium Server was failing.

    I resolved this problem by providing explicit path to the Firefox 2 executable file in the java code. (I also tried providing explicit path to the Firefox 3 executable but that is no help).

    So, the code for the Java Selenium test-case looks like this :

    selenium = new DefaultSelenium("localhost", 4444, "*firefox /usr/lib/firefox/firefox-2-bin", url);

    This is not a solution, if you want to test your app with Firefox 3. I haven’t figured out the solution for making it work with Firefox 3; but for the time being, I am happy with testing my application with Firefox 2.

    If somebody has a solution for making it work with Firefox 3, please post the solution as a comment to the blog.

    -Deepak

    • Share/Bookmark
  • Installing IEs4Linux on Ubuntu Gutsy Gibbon

    23 Apr 2008 in Java tools& Linux

    There are certain sites and applications which work only with Internet Explorer. One such application that I am currently working on is EMC Documentum Webtop.

    After getting fed-up of the warning message thrown by Webtop about unsupported browser and some weird behavior sometimes, I finally decided to install IE on my Ubuntu machine.

    I followed the following steps to install IE on my Ubuntu machine

    • modified /etc/apt/sources file to add/un-comment the following 2 lines

    deb http://in.archive.ubuntu.com/ubuntu gutsy universe
    deb-src http://in.archive.ubuntu.com/ubuntu gutsy universe
    deb http://wine.budgetdedicated.com/apt gutsy main
    deb-src http://wine.budgetdedicated.com/apt gutsy main

    • sudo apt-get update
    • sudo apt-get install wine cabextract
    • wget http://www.tatanka.com.br/ies4linux/downloads/ies4linux-latest.tar.gz
    • tar xzvf ies4linux-2.0.5.tar.gz
    • cd ies4linux-2.0.5
    • ./ies4linux (make sure you are not root here)

    When running the installer (the last step), I got the following error

    ui/pygtk/python-gtk.sh: line 6: 15717 Segmentation fault (core dumped) python "$IES4LINUX"/ui/pygtk/ies4linux-gtk.py

    After googling around to find a solution to the problem, I had a look at the ies4linux-gtk.py file mentioned in the error message. The line 6 looks like this

    import gtk, gobject, pango, sys, os

    Somehow, I suspected that there are some unmet dependencies, so looked for packages by the name gobject

    apt-cache search gobject

    and I found this:

    gob2 – GTK+ Object Builder

    I installed the gob2 package

    sudo apt-get install gob2

    and after that I ran the installer again and this time the installation succeeded without any problems.

    Hope this helps somebody.

    • Share/Bookmark
  • Running VMWare on AMD 64 running Feisty Fawn

    08 Apr 2008 in Linux

    If you are running Feisty Fawn on AMD 64 machine and try to open a VMWare image, you will most-likely get this error message

    “Error while powering on: Failed to connect to peer process.”

    The solution to this problem is to install the following the 32-bit libraries required by Ubuntu to recognize the 32-bit operating system.

    Use the following command to install the required libraries.

    sudo apt-get install ia32-libs

    After installing these libraries, I was able to open the virtual machine without any problems on my Ubuntu Feisty Fawn AMD 64 machine.

    Hope this helps somebody.

    • Share/Bookmark