Alfresco: How to write a simple Java based Alfresco web script?

If you want to develop a new feature for Alfresco best way is WebScript! Let's start with a simple Alfresco web script. First, you need to create an Alfresco AMP maven project using archetype. In this example, I'll use the latest alfresco version 5.0.

First I generated Alfresco All-in-One AMP. (Please refer my blog post on generating AMP projects.)

If you go through the files structure which is generated, you will find out a sample web script. It is a JavaScript-based WebScript. By this example, I'm going to explain how to write a simple Java-based Hello World web script.

HelloWorldWebScript.java service-context.xml helloworld.get.desc.xml helloworld.get.html.ftl
Create the above files in below locations of your maven project.
  • HelloWorldWebScript.java - repo-amp/src/main/java/org/dedunu/alfresco/HelloWorldWebScript.java
  • helloworld.get.desc.xml - repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts/org/dedunu/alfresco/helloworld.get.desc.xml
  • helloworld.get.html.ftl - repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts/org/dedunu/alfresco/helloworld.get.html.ftl
  • service-context.xml - repo-amp/src/main/amp/config/alfresco/module/repo-amp/context/service-context.xml
Use below command to run the maven project.

mvn clean install -Prun 

It may take a while to run the project after that open a browser window. Then visit below URL

http://localhost:8080/alfresco/service/dedunu/helloworld.