Custom request/form submission with Groovy scripts

Often you will want to capture data from forms and perform an action, such as sending mail after submission of a contact form. In Weceem this is achieved by writing some Groovy code that you store in the content repository. This happens dynamically and you don't need to redeploy your Weceem application or restart it.

Two content types work together in Weceem to enable you to write arbitrary server-side code. The Scripted Action node type is used to receive requests, and delegates the work to a Groovy Script node.

So for example to create a Contact form that sends mail to a known address, you could implement it like this:

  1. Create a Groovy Script node in your repository, and paste into it the following code:

    def args = [
      recipients:'admin@yourserver.com',
      subject:'Message from website visitor'
    ]
    try {
      sendMail {
        to args.recipients
        if (args.ccRecipients) {
          cc args.ccRecipients
        }
        subject args.subject

        from params.senderAddress
        body params.message
      }
      redirect(uri:params.success)
    } catch (Exception e) {
      log.error "Can't send mail", e
      redirect(uri:params.error)
    }

     
    This code expects the Grails "mail" plugin to be installed (as it is by default in the Weceem application WAR file). This code is exactly what you would write if you were writing your own Grails controller action. You can call redirect, render and other Grails controller methods as usual. The request parameters are in "params".
  2. Now you have a script to run, you need to create a Scripted Action node to respond to requests for a specific URI and run the script. When you do this, you select the script you just created in the "Script" field. The full repository URI of this Scripted Action content node is the URI that needs to be used in your HTML form's action attribute. You can create links to this action inside your own Widget or Template nodes using the wcm:createLink tag.

With the above example, the HTML form needs to supply fields "senderAddress" and "message". It will redirect the user to the value of the "success" parameter, which will usually be a URI in your content repository created using the wcm:createLink tag.

Note that you can write any Groovy code you like in the Groovy Script node. The possibilities are endless - you can call out to external web services, perform calculations, render complex results in alternative formats (e.g. JSON).

As ever, with great power comes great responsibility. You can easily degrade the performance of your server by writing inefficient code in a Groovy Script that is executed frequently.

It is strongly recommended that you limit the users that can create Groovy Scripts using the Weceem Security Policy.