alfresco Hello World Web Script


Example

Let's make a hello world web script. Web scripts have a descriptor, a controller, and, optionally, a view. These files must follow a naming convention.

This descriptor is named helloworld.get.desc.xml.

<webscript>
  <shortname>Hello World</shortname>
  <description>Hello world web script</description>
  <url>/example/helloworld?name={nameArgument}</url>
  <authentication>user</authentication>  
</webscript>

You can see that the descriptor declares that this web script will be mapped to a URL, "/example/helloworld", and that it requires user authentication. The descriptor also declares an argument called name.

Here is the controller. It is named helloworld.get.js.

model.foo = "bar";

This controller is written in JavaScript but controllers can also be written in Java. With a bit more work you can write controllers in other languages too.

This controller doesn't do much. It just adds a new variable to the model called "foo" and gives it a value of "bar".

Your controller has access to a variety of root scoped variables which are all documented in the official documentation.

Finally, let's look at the view. It's named helloworld.get.html.ftl

<html>
  <body>
    <p>Hello, ${args.name!"name not specified"}!</p>
    <p>Foo: ${foo}</p>
  </body>
</html>

You can see from the name that this view is implemented as a Freemarker template and outputs HTML. This view grabs the value of "foo" from the model and it also grabs the name argument that was passed in to the web script. If a name argument is not specified the template provides some default text.

If you wanted to produce XML or JSON instead you can--just change the name, then update your template implementation accordingly.

Deployment

Web scripts can be deployed to the classpath or uploaded to the repository. For example, to deploy this web script by uploading to the repository, follow these steps:

  1. Upload these three files to Data Dictionary/Web Scripts Extensions
  2. Refresh the Web Scripts by going to http://localhost:8080/alfresco/s/index and clicking "Refresh Web Scripts".
  3. Navigate to the web script by going to http://localhost:8080/alfresco/s/example/helloworld?name=Jeff