cancel
Showing results for 
Search instead for 
Did you mean: 

Dashlet: how to use java-backed code to instead of the .js controller?

sunquanbin
Champ on-the-rise
Champ on-the-rise
Hi,

I have created a very simple dashlet for share. I have the following three files in the path: alfresco/web-extension/site-webscripts/org/alfresco/components/dashlets

<li>A controller: hello.js</li>
  It defines a porpterty:
model.msg = "A hello message."


<li>A free marker template: hello.get.html.ftl.</li>
  It shows the message:
${msg}


<li>A descriptor: hello.get.desc.xml</li>


I tested share and I could add this dashlet successfully.

My question is: can I write a java code as a controller to return the "model.msg" object instead of using the "hello.js". Something is similar in Java Backed WebScript, which registers the java class to an http get request in a *-context.xml file:

<beans>
  <bean id="webscript.org.alfresco.component.dashlets.hello.get" class="org.alfresco.demo.HelloJava" parent="webscript" />
</beans>


Is there a similar way to do this for dashlet? And how should I name this ???-context.xml file and where should I put it?

PS. my java code is


public class HelloJava extends DeclarativeWebScript
{
   @Override
   protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
   {
      Map<String, Object> model = new HashMap<String, Object>();
      model.put("msg", "A message from Java Code.");
      
      return model;
   }
}



Many thanks

Sun
6 REPLIES 6

eric_soto
Champ in-the-making
Champ in-the-making
Sun Quanbin,

Yes, you can write Java code as a controller to return the "model.msg" object instead of using hello.js. Assuming that your bean id is correct and that your webscript package is in fact org.alfresco.component.dashlets and that your Java package is in fact org.alfresco.demo, yes it should work. Your context file can be named "custom-slingshot-context.xml" and can be placed in your web-extension directory. Just to be absolutely sure I verified this in my local environments (Alfresco Enterprise 4.0.2.9 and 4.1.5) and it did work.

My question for you is why would you want to do that? To quote MikeH: "The preferred way would be to expose your Alfresco (repository) version as a RESTful service and call it from Share that way". For more on this please follow the following thread: Click here

<strong>Note</strong>: I believe that "msg" is a reserved function name in the Javascript webscript. To be safe I would change it to "message" or something other than "msg". The same goes for the Java class; change the variable name that goes into the model map. Also, don't forget to change the Freemarker template variable name to ${message}, or whatever you changed the name to in the Java class.

I hope this helps. Cheers.

Thanks for the reply, but I cannot get things work. I'm very new to Alfresco, could you describe the steps in more detail?

My descriptor looks like:


<webscript>
   <shortname>demo</shortname>
   <description>A placeholder for demo materials</description>
   <family>site-dashlet</family>
   <url>/components/dashlets/demo</url>
</webscript>


And I registered it in web-extension/custom-slingshot-application-context.xml:

<beans>
   <bean id="webscript.org.alfresco.components.dashlets.hello.get"
      class="org.alfresco.demo.DemoJava" parent="webscript" />      
</beans>

Hi Eric,

I am facing similar issues with share java backed webscripts & mentioned it @ http://forums.alfresco.com/forum/end-user-discussions/alfresco-share/alfresco-share-java-backed-webs....

Can you help?

Regards.

eric_soto
Champ in-the-making
Champ in-the-making
Sun,

I just realized that you are extending Share using the "shared/classes/alfresco/web-extension" directory instead of using an AMP (Alfresco Module Package) file. To quote the Packaging And Deploying Extensions wiki: <em>"Because of web application sandboxing, Java classes which extend Alfresco's Java classes cannot be placed in the shared class loader. They must be in the class loader for the web application to which they apply."</em> For this I would suggest packaging your extension as an AMP, and injecting it into your current war file. When exploded, the new war file will copy a jar containing your compiled code to the "WEB-INF/lib" directory.

The point I would like to get across is that Java-backed webscripts really shouldn't exist in Share; they should be in the Alfresco Repository. Share, through a presentation webscript (or Ajax call), should then access the data webscript (Java-backed webscript). Since you asked if it was possible to do I told you it was, but more importantly you should know it's generally not considered a best-practice. Java is for the heavy-lifting of data and, as such, they should reside as webscripts in the Repository tier–not the GUI/presentation tier (i.e. Share).

I hope this helps clears things up.

eric_soto
Champ in-the-making
Champ in-the-making
Sun,

You might be interested in how to build your own Share development project in Eclipse (or any other integrated development environment). You can click here to visit the share-extras website which contains a sample Share project with a sample dashlet which you can quickly convert to a Java-backed webscript/dashlet. Share-extras is a good place to start to if you are new to developing in Alfresco and Share and want to learn how to build extension modules and how to deploy them as AMPs. A lot of what I have learned came from studying those same examples. It gives you enough information to get started and to build on as needed.

I wish you much success! Cheers.

Eric,

Sorry for the confusion here. I am using the Maven SDK (then imported into eclipse) to build AMP for Alfresco and Share. My current understanding is:
<li>if the AMP is for Alfresco.war, create an"extension" folder under /src/main/amp/config/alfresco </li>
<li>if the AMP is for Share.war, create a "web-extension" folder under /src/main/amp/config/alfresco </li>

Back to my question, I think you have answered it in this post: there is no way to create a Java code controller for dashlet in Share. If I may sum it up, for a dashlet, it has to call the services which created in Alfresco Repository in its own *.js controller.