Thursday, November 18, 2010

Alfresco @MVC

I was never happy with webscripts and I always preferred using Java to Javascript server-side programming ... but that is just a personal opinion and preferences.

Alfresco 3.4 came with Springframework 3, I have to say that Alfresco has been really slow in adapting newer versions of different technologies and probably they will remain, but this move is a good one and I was waiting for that for a long time. A lot of people are using Springframewok and especially the @MVC annotations and now we can do the same with Alfresco, of course not out of the box.

To achieve that I wrote a webscript which enables the usage of Spring @MVC inside of any Alfresco modules (AMP). Sure, I must admit that Alfresco Share is using it but I am a partisan of using Alfresco as a platform and not only as a repository, therefore I prefer writing java code integrated with the Alfresco Java API. This way of working opens huge possibilities and you can be much faster in developing new controllers than new webscripts. This approach is a standalone webscript design and let Spring @MVC do the rest for you.

I did not yet published the code but will be done soon at Google code. However here is a preview of what you can do ...

Firstly you need to initialize the webscript as you would do with any other webscript

<bean id="webscript.com.gradecak.spring.dispatch.post" class="com.gradecak.spring.webscript.DispatcherWebScript" parent="webscript" scope="prototype">

<property name="contextConfigLocation" value="/WEB-INF/classes/alfresco/module/com.gradecak.spring/context/dispatcher/servlet-context.xml">

</property>

</bean>


than in the servlet-context.xml you simply enable Spring annotations and use all the Spring config you want ...

...
<context:annotation-config />

<context:component-scan base-package="com.gradecak.spring.controller" />

...


and than in the controller you are free to use Alfresco's Java API or any library that you want

@Controller
@RequestMapping("/user/*")
public class UserController {

@RequestMapping(value = "get", method = RequestMethod.GET)

public String getUser(@RequestParam String userId) {
....

}

}


Sure, some minor problems appear if you want to use Freemarker as the view but that is just a matter of having Alfresco include a newer Freemarker JAR file and if you want to use a JSON view, Spring is coming with Jackson, but for a higher integration we need a later version of Spring than 3.0.0 provided by Alfresco ... so let's the show go on ;)