cancel
Showing results for 
Search instead for 
Did you mean: 

Spring MVC Controllers vs. Webscripts

mangar
Star Contributor
Star Contributor
I recently was able to add spring controllers to my Alfresco.  I just added this to my web.xml:

   <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/springMVC/*</url-pattern>
    </servlet-mapping>


Added a springMVC-servlet.xml like this: (only important bits:

    <context:annotation-config />
    <context:component-scan base-package="org.xxx.alfresco.web"/>
    <mvc:annotation-driven />


and presto!  SpringMVC Controllers in my Alfresco.  I can even easily inject the registry, nodeservice, etc…. (Actually, the @Autowired annotation works so it's so easy and supercool.)

This seems A LOT simpler than the webscript framework.  More powerfull/flexible, I can do views/templates.  Yes I understand that I can do all of this in a webscript, but SpringMVC is SO much easier.

Now, I did a quick search on the internets and found nothing on this technique. Which makes me think one of 3 things: (in ascending order of likelihood)

1. I am a ground breaking genius.
2. This is obvious, been done many times, and so simple no tutorials are necessary. 
3. There is something horribly wrong with this technique.

I assume that it is #3, but what do you think?

Any feedback would be greatly appreciated.
5 REPLIES 5

mrogers
Star Contributor
Star Contributor
There's nothing inherently wrong.   However you are essentially re-inventing the wheel.  I'm surprised you think it's so much easier, web scripts are supposed to be simple and easy too.

You will need to handle your transactions and exceptions correctly ( with a retrying transaction helper ) so you will also need to reinvent some framework code to go on top of sping mvc.

mangar
Star Contributor
Star Contributor
Well, I think web scripts are easy, but I am working with some VERY jr programmers whose only Java exposure is SpringMVC. So I thought it would be easier for them.

So if I inject, say, the node service into my web script, how is that different, transaction wise, from using it in a SpringMVC controller?

abarisone
Star Contributor
Star Contributor
Hi,
I've been a Java developer for many years and love Spring and its MVC implementation.
Having said that, I experimented webscripts with Alfresco and one of the main advantages I found was that I was able to modify my webscripts and test them simply refreshing the index page. This doesn't mean that I quit using Java and Spring, but this brought to me another point of view about developing.
And remember that Alfresco always runs Spring under the hood, giving you all its power.
Well your developers should get acquainted with Javascript and Freemarker templates, but might save a lot of time recompiling and redeploying the code.

Regards,
Andrea

mrogers
Star Contributor
Star Contributor
Imagine you have a MVC controller that does two calls to the repository.   Without re-inventing a little of the framework code there's the possibility call1 will succeed and call2 fails.     That may or may not be a problem but at some point will hurt.   So you need to make sure your Spring MNC controller is using the RetryingTransactionHelper correctly,

You can also have alfresco webscripts with Java implementations if the use of alfresco script is a concern. 

daniel_gradecak
Confirmed Champ
Confirmed Champ

Hi,

I am aware the thread is old, but wanted to let you know about GitHub - dgradecak/alfresco-mvc that is a project I developed and actually is the glue code between alfresco and spring MVC. I am also using it on the share side and expose spring-data-rest for instance. On the repository side I wrote another module that uses alfresco @mvc to expose something that I call spring-data-alfresco and is actually exposing spring data repositories to be used within alfresco with actually an implementation that access alfresco as a spring data repository resource.

But I highly recommend to check the original production ready and used in many projects (not by me only) Alfresco @MVC project.

The wrong approach with what you have in the original post is that you do not use the webscript framwork, so no authentication etc are taken in consideration by alfresco., The module I am talking about is actually a webscript that uses a DispatcherServlet under the hood and therfore all the alfresco advices etc are being applied to every request to a spring controllers. And the lasts thing it has a pretty useful annotation to annotate services @AlfrescoTransaction (and some more).

So you are not alone in this quest