Alfresco webscripts provide a great power to extend or consume Alfresco repository and thus make it easy to integrate with other web application or other repositories. Now we had seen most of the things about webscripts in these posts.
Here I will mainly focus on java based webscripts. Alfresco javabased webscript are called java backed because the controller of webscript is java class where all our business logic will reside and it will create model and forward it to the views (template files). Java backed webscripts are very powerful compare to other javascript based webscript because you can inject alfresco services within webscript bean definition because our backend java controller is nothing but a spring bean only and alfresco repository services are also created within spring so we can wire them up and utilize those services (nodeservice,cotentservice,authentication service etc…). These services has rich set of APIs available with them so using them we can manipulate repository to great extent.
There are two ways to implement java backend controller
- Extends AbstractWebscript
- Extend DeclarativeWebScript
Let us see main difference between both of above implementation in brief.
AbstactWebscript | DeclarativeWebScript |
Abstract Class | Script/template driven based implementation of an Web Script |
extends Object Class andimplements WebScript Interface | extends AbstractWebScript |
Important Methods areExecute | Important MethodsExecute,executeImpl,executeFinallyImpl,renderFormatTemplate |
Here we can write directly to output because we have direct control over response object | In this case we normally create model and pass it to the view and view will render output. |
Sample Webscript Implementations
package com.arvixe.sample.webscripts; import java.io.IOException; import org.springframework.extensions.webscripts.AbstractWebScript; import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; public class MyAbstractWebScript extends AbstractWebScript { public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException { //All your business logic goes here //Create json or xml response string here String responseStr; // write response string directly using response object res.getWriter().write(responseStr); } }
Declarative webscript example
package com.arvixe.sample.webscript; import java.util.HashMap; import java.util.Map; import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.DeclarativeWebScript; import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.WebScriptRequest; public class MyDeclarativeWebscript extends DeclarativeWebScript { @Override protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { //put all the objects that you need in renditions to the model map // You can pass map of resultset nodeRefs so that they can be //iterated in ftl files Map<String, Object> model = new HashMap<String, Object>(); model.put("test", "Testing string"); return model; } }
Decelarative webscript has a big advantage over Abstract webscript because here we can pass model to view so we could have multiple different views for single webscript.
Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!
Hi Thank you for good post ,
Can you please help me to know how exactly to write this java code and use maven and eclipse to develop webscripts.?
There are few posts on this but all of them confusing me on java backed webscripts.
Your posts always helps me to understand easily.
Hi Aditya,
For setting up maven environment in eclipse for Alfresco development you can refer this post
http://ecmarchitect.com/alfresco-developer-series-tutorials/maven-sdk/tutorial/tutorial.html
Then for Developing webscript you can refer my previous posts related to webscripts.