星期四, 3月 29, 2012

[Alfresco] How to create Java backed web script in alfresco (2)

痛過的經驗記錄一下。
看過上一篇的Java backed 架構圖之後,
得知我們需要完成四個Components,
分別是
1.XML descriptor document (Describing the web script)
2.Freemarker (Writing the response template-FTL)
3.Java class (Writing the web script controller)
4.Spring bean XML(Register the Java class in the Alfresco)



準備Web Script Controller
1.首先先開一個新的Java project,例如 AlfrescoJavaBackedWebScript
2.新建一個User library,例如Alfresco,把alfresco lib裡面你需要的jars都add進去
3.新建org.example的package
4.新建一個SimpleDeclarativeWebscript.java,內容如下
package org.example;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 SimpleDeclarativeWebscript extends DeclarativeWebScript {

 private static Log s_logger = LogFactory.getLog(SimpleDeclarativeWebscript.class);


 @Override
 protected Map<string, object> executeImpl(WebScriptRequest req, Status status, Cache cache) {

  s_logger.debug("SimpleDeclarativeWebscript entry");

  //put all the objects that you need in renditions to the model map
    Map<string, object> model = new HashMap<string, object>();
  model.put("msg", "Hello!!Java-Backed web script");
  return model;
 }
}



5.將專案build成一個jar檔:cosa-javabackedwebscript.jar,這樣controller就準備好了!!
6.將這個jar檔放在這路徑下,千萬別亂放!!


/usr/local/TOMCAT/webapps/alfresco/WEB-INF/lib


準備Xml Descriptor Document與Freemarker樣版
1.依據你的Java package 路徑建立這二個檔案要放置的目錄結構:本例為 org/example
完整路徑為

/usr/local/TOMCAT/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/example

2.在org/example路徑下建立simpledeclarative.get.desc.xml內容如下
<webscript>
   <shortname>Hello Javabacked(Extend DeclarativeWebScript)</shortname>
   <description>
   This is first java backed web script in COSA.
   author:ibigd
   </description>
   <url>/cosa/sample/javabacked/declarehello</url>
   <authentication>none</authentication>
   <transaction>required</transaction>
   <format default="json">any</format>
   <family>Alfresco java backed webscripts demo</family>
</webscript>

3.在org/example路徑下建立樣版,simpledeclarative.get.json.ftl 
{ "javaclass":"SimpleDeclarativeWebScript", "statusCode" : ${status.code}, "msg":${msg} }



準備Spring Bean XML綁定你的Java backed
1.在以下的路徑內,建立cosa-javabackedwebscript-context.xml
/usr/local/TOMCAT/webapps/alfresco/WEB-INF/classes/alfresco/extension
內容如下:


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
   <!--Java backed webscrips test -->
      

        <bean
         id="webscript.org.example.simpledeclarative.get"
         class="org.example.SimpleDeclarativeWebscript"
         parent="webscript">
        </bean>

</beans>


注意事項:Bean tag的命名規則

  • id: webscript.<web script package>.<web script name>.<http method>
    • <web script package>代表你的web script的目錄路徑,建的路徑要跟你java package的階層一樣 Update 12/6/25:只要正確指對路徑即可
    • <web script name> 使用java class來命中(目前測試都用小寫)
    • <http method> http 的呼叫方法: get, put, post, delete
  • class: <java package name>.<java class name> (請注意這裡的物件名稱的命名不能錯,大小寫不能錯誤)
  • parent: webscript
接下的範例將介紹如何injection spring bean,這樣就能直接呼叫alfresco的核心API.。

請參考part3[Alfresco] How to create Java backed web script in alfresco (3)

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails