Monday Apr 21, 2008

TOTD #30: CRUD Application using Grails - Hosted on Jetty and HSQLDB


After a simple Grails application, let's create a CRUD application. Such an application allows to perform basic database operations to read table rows from the database, create new rows, and edit and delete existing rows. This blog shows how such an application can be created using Grails, hosted on built-in Jetty servlet engine and use in-memory HSQLDB database for persistence.

A follow-up entry will show how this application can be deployed in production mode on GlassFish and using MySQL database.
  1. Create a Grails application
    1. After Grails download and configuration, create a new application "crud" as:

      ~/testbed/grails-1.0.2/samples >grails create-app crud

      Welcome to Grails 1.0.2 - http://grails.org/
      Licensed under Apache Standard License 2.0
      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples
      Note: No plugin scripts found
      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateApp.groovy
      Environment set to development
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src/java
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/src/groovy
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/controllers
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/services
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/domain
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/taglib
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/utils
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views/layouts
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/i18n
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/unit
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/scripts
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/js
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/css
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/images
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/META-INF
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/lib
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf/spring
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/conf/hibernate
      [propertyfile] Creating new property file: /Users/arungupta/testbed/grails-1.0.2/samples/crud/application.properties
           [copy] Copying 2 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud
           [copy] Copying 2 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/WEB-INF
           [copy] Copying 5 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/WEB-INF/tld
           [copy] Copying 87 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app
           [copy] Copying 17 files to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud
      [propertyfile] Updating property file: /Users/arungupta/testbed/grails-1.0.2/samples/crud/application.properties
      Created Grails Application at /Users/arungupta/testbed/grails-1.0.2/samples/crud
    2. In your project directory, create a domain class as:

      ~/testbed/grails-1.0.2/samples/crud >grails create-domain-class state

      Welcome to Grails 1.0.2 - http://grails.org/
      Licensed under Apache Standard License 2.0
      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
      Note: No plugin scripts found
      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateDomainClass.groovy
      Environment set to development
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/domain
      Created  for State
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
      Created Tests for State

      This creates "State.groovy" class in "grails-app/domain" directory and looks like:

      class State {

      }

      Add two fields to store state name and the corresponding abbreviation as shown below:

      class State {
              String name
              String abbrev
      }
    3. Change "grails-app/conf/BootStrap.groovy" class to initialize the domain with sample data as shown below:

      class BootStrap {

           def init = { servletContext ->
                new State(name:"California", abbrev:"CA").save()
                new State(name:"New York", abbrev:"NY").save()
                new State(name:"Texas", abbrev:"TX").save()
                new State(name:"Wisconsin", abbrev:"WI").save()
           }
           def destroy = {
           }
    4. Create a new controller as:

      ~/testbed/grails-1.0.2/samples/crud >grails create-controller state

      Welcome to Grails 1.0.2 - http://grails.org/
      Licensed under Apache Standard License 2.0
      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
      Note: No plugin scripts found
      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/CreateController.groovy
      Environment set to development
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/controllers
      Created Controller for State
          [mkdir] Created dir: /Users/arungupta/testbed/grails-1.0.2/samples/crud/grails-app/views/state
           [copy] Copying 1 file to /Users/arungupta/testbed/grails-1.0.2/samples/crud/test/integration
      Created ControllerTests for State

      This generates "grails-app/controllers/StateController.groovy" with the following content:

      class StateController {

          def index = { }
      }

      Replace it with to define a scaffold:

      class StateController {

          def scaffold = State
      }
  2. Start the application using built-in Jetty Servlet engine and in-memory HSQLDB database as shown below:

    ~/testbed/grails-1.0.2/samples/crud >grails run-app

    Welcome to Grails 1.0.2 - http://grails.org/
    Licensed under Apache Standard License 2.0
    Grails home is set to: /Users/arungupta/testbed/grails-1.0.2

    Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/crud
    Note: No plugin scripts found
    Running script /Users/arungupta/testbed/grails-1.0.2/scripts/RunApp.groovy
    Environment set to development
    Running Grails application..
    2008-04-18 17:26:29.962::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
    2008-04-18 17:26:29.075::INFO:  jetty-6.1.4
    2008-04-18 17:26:29.155::INFO:  No Transaction manager found - if your webapp requires one, please configure one.
    2008-04-18 17:26:30.886:/crud:INFO:  Set web app root system property: 'crud' = [/Users/arungupta/testbed/grails-1.0.2/samples/crud/web-app/]
    2008-04-18 17:26:30.886:/crud:INFO:  Initializing Log4J from [file:/Users/arungupta/.grails/1.0.2/projects/crud/resources/log4j.properties]
    2008-04-18 17:26:30.945:/crud:INFO:  Initializing Spring root WebApplicationContext
    [0] spring.GrailsWebApplicationContext Refreshing org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb: display name [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb]; startup date [Fri Apr 18 17:26:32 PDT 2008]; parent: org.springframework.web.context.support.XmlWebApplicationContext@cddcc3
    [1] spring.GrailsWebApplicationContext Bean factory for application context [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@848dfb]: org.springframework.beans.factory.support.DefaultListableBeanFactory@11f136
    2008-04-18 17:26:34.655:/crud:INFO:  Initializing Spring FrameworkServlet 'grails'
    2008-04-18 17:26:35.716::INFO:  Started SelectChannelConnector@0.0.0.0:8080
    Server running. Browse to http://localhost:8080/crud

    The application is now accessible at "http://localhost:8080/crud" and looks like:



  3. READ - Click on "StateController" and the following page is shown:



  4. CREATE - Click on "New State" and enter the values as shown below:



    and click on "Create" to see the following page:



  5. UPDATE & DELETE - You can click on "Edit" or "Delete" to perform U or D of CRUD or click on "State List" to view the updated list as shown below:

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

Technorati: groovy grails glassfish jetty hsqldb scripting crud

Wednesday May 23, 2007

Tango on Jetty

Web Services Interoperability Technology (WSIT, aka Project Tango), integrated in GlassFish V2 builds, provides a implementation of key enterprise Web services specifications and enables first-class interoperability with .NET 3.0 framework. However if you download stand-alone WSIT builds, then you can also install similar bits on Tomcat 5.5.17. In this entry, I provide a script that allows you to install the same bits on Jetty 6.1.0.

 

  1. Download WSIT Milestone 4 and J2SE 5.0 U2 or later.
  2. Install (detailed instructions) the bundle by giving the following command:

    java -jar wsit-1_0-fcs-bin-b14-09_apr_2007.jar
  3. Copy the script below in 'jax-ws-latest-wsit' directory and name it as 'wsit-on-jetty.xml':

    <?xml version="1.0"?>

    <project name="WSIT 1.0 Beta On Jetty" default="help" basedir=".">
      <property environment="env"/>
      <property name="jetty.home" value="${env.JETTY_HOME}"/>
      <property name="jetty.lib.home" value="${jetty.home}/lib"/>
      <property name="java.home" value="${env.JAVA_HOME}"/>
      <patternset id="wsit-jars">
        <include name="webservices-rt.jar"/>
        <include name="webservices-tools.jar"/>
        <include name="webservices-extra.jar"/>
        <include name="webservices-extra-api.jar"/>
        <include name="webservices-api.jar"/>
      </patternset>

      <target name="install" description="Install latest WSIT jars on Jetty 6.1.x">
        <echo message="Installing WSIT 1.0 Beta on ${jetty.home} ..."/>

        <copy toDir="${jetty.lib.home}" overwrite="true">
          <fileset dir="lib">
            <patternset refid="wsit-jars"/>
          </fileset>
        </copy>
        <echo message="... installation complete."/>
      </target>

      <target name="uninstall" description="Install latest WSIT jars on Jetty 6.1.x">
        <delete>
          <fileset dir="${jetty.lib.home}">
            <patternset refid="wsit-jars"/>
          </fileset>
        </delete>
      </target>

      <target name="help">
        <echo message="install :"/>
        <echo message=" Installs WSIT 1.0 Beta on Jetty 6.1.x"/>
        <echo/>
        <echo message="uninstall: "/>
        <echo message=" Uninstalls WSIT 1.0 Beta from Jetty 6.1.x"/>
        <echo/>
        <echo message="$JETTY_HOME must be set to the installation directory of Jetty 6.1.x."/>
        <echo/>
        <echo message="Usage:"/>
        <echo message=" ant -f wsit-on-jetty.xml {install,uninstall}"/>
      </target>
    </project>
  4. Download and install (basically unzip the downloaded file) Jetty 6.1.0. Set an environment variable JETTY_HOME pointing to the location of Jetty install directory.
  5. Invoke the command to install WSIT M4 bits on Jetty as:

    ant -f wsit-on-jetty.xml install
  6. Create a Reliable WSIT endpoint by choosing 'J2EE 1.4' as the 'Java EE version' and make sure 'Set Source Level to 1.4' is unchecked.
  7. Copy the WAR file from the 'dist' directory of your application such '\\Users\\Arun Gupta\\WebApplication1\\dist\\WebApplication1.war' to 'JETTY_HOME/webapps'.
  8. Start Jetty instance using the following command:

    java -jar start.jar etc/jetty.xml

    It's weird that 'bin' directory contains only 'jetty.sh' (no .bat script) and the only way documented to run is using this command.
  9. The endpoint should now be available at: 'http://localhost:8080/<context-root>/<service-name>?WSDL', for example 'http://localhost:8080/WebApplication1/HelloService?WSDL'. Note, Jetty does not seem to support hot deployment. So if you drop a WAR file in 'webapps' directory then you need to re-start your Jetty instance.

  10. A Web service client to this endpoint can be easily generated following #ws2 screencast.

This is one trivial sample. If you try other interesting combinations and they don't work, please file an issue.

Technorati: wsit glassfish webservices jetty

About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today