- Learning Alfresco Web Scripts
- Ramesh Chauhan
- 517字
- 2021-08-05 17:28:05
Extending the first web script to use the controller
The web script framework in Alfresco makes it easy to have a clear separation of concerns by following a model-view-controller pattern in order to develop a web script. All the business logic resides in the controller and it is possible to have multiple views as required to return the response in different response formats supported by the web script framework in Alfresco. The model object is a data structure used to pass information from the controller to the view. The controller populates the model object with the required data and passes it to the view to generate the response.
In the first web script, we created a basic and simple web script, which just renders the output without interacting with the Alfresco repository. Ideally, any processing related to business logic such as querying the Alfresco repository, creation of content, updating content, deleting content from the repository, and executing actions should be done in a controller. A controller could be a server-side JavaScript or a Java-based controller.
Let's extend our first web script to add a controller to get an understanding about how the controller populates and passes data to FreeMarker template and how FreeMarker template uses the model data to generate the response:
- Log in to Alfresco Share UI.
- Click on the Repository link from the top header.
- Go to Data Dictionary | Web Scripts Extensions.
- Edit the descriptor file
helloworld.get.desc.xml
as follows:<webscript> <shortname>Hello World</shortname> <description>First webscript Hello world</description> <url>/helloworld?name={argumentName}</url> <authentication>user</authentication> </webscript>
- Create a new file named
helloworld.get.js
with content as follows:model.email="Ramesh.chauhan@cignex.com";
- Edit the FreeMarker template
helloworld.get.html.ftl
as follows:<html> <body> <p>Hello! ${args.name}.</p> <p>email address: ${email}</p> </body> </html>
- Go to
http://localhost:8080/alfresco/service/index
and click on the Refresh Web Scripts button. - Now, hit the URL
http://localhost:8080/alfresco/service/helloworld?name=Ramesh
to execute the web script. It will ask for user credentials. Provide user credentials and you should see the response as:Hello! Ramesh.
email address: Ramesh.chauhan@cignex.com
This is how we can have a controller added in a web script in a simple way. In the preceding example, we hard-coded the e-mail address to be returned in the web script response. We did not have any interaction with the Alfresco repository. We just populated the model object and passed it to the FreeMarker template from the server-side JavaScript controller.
Note
Model is basically a root object that Alfresco makes available for data sharing to happen between the server-side JavaScript controller and FreeMarker template view.
You can modify the controller to get the e-mail address dynamically retrieved from the Alfresco repository based on the name argument received. With the help of Alfresco JavaScript API, you may first retrieve the user from the repository based on the name argument and then fetch the e-mail address of the user. In a server-side JavaScript controller, you can leverage on available Alfresco JavaScript API to interact with the repository. This way, we can have a web script render a response based on the data retrieved from the Alfresco repository.
Note
In a server-side JavaScript controller, you can leverage on available Alfresco JavaScript API to access the Alfresco repository and perform business logic as required.
- Spring 5.0 Microservices(Second Edition)
- Mastering Entity Framework Core 2.0
- Spring 5.0 By Example
- UML和模式應(yīng)用(原書第3版)
- Java 9 Programming Blueprints
- 編寫高質(zhì)量代碼:改善C程序代碼的125個(gè)建議
- C語(yǔ)言程序設(shè)計(jì)
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲案例實(shí)戰(zhàn)全流程詳解(高級(jí)進(jìn)階篇)
- Symfony2 Essentials
- Android開發(fā):從0到1 (清華開發(fā)者書庫(kù))
- Arduino家居安全系統(tǒng)構(gòu)建實(shí)戰(zhàn)
- PHP+MySQL+Dreamweaver動(dòng)態(tài)網(wǎng)站開發(fā)從入門到精通(第3版)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)案例教程
- Maker基地嘉年華:玩轉(zhuǎn)樂動(dòng)魔盒學(xué)Scratch
- Scratch從入門到精通