Outputtng html by using Ballerina

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 06:59:24

问题


Is there any way to output a html page with some data in it by using Ballerina language?

Assume that I need the "orderid" string in the below code to be displayed inside a H1 tag in a HTML page..

import ballerina.net.http;
import ballerina.lang.system;

@http:BasePath {value:"/shop"}
service echo {

    @http:GET{}
    @http:Path {value:"/order"}
    resource echoGet (message m, @http:QueryParam {value:"orderid"}string orderid) {
        http:convertToResponse(m);
        system:println("orderid" + orderid);
        reply m;

    }
}

回答1:


Ballerina is an Integration Language and hence does not support such functionality. However, as an alternative, you can try something as below.

import ballerina.net.http;
import ballerina.lang.messages;
import ballerina.lang.xmls;

@http:BasePath {value:"/shop"}
service echo {

    @http:GET{}
    @http:Path {value:"/order"}
    resource echoGet (message m,@http:QueryParam {value:"orderid"}string orderid) {
        xml xmlPayload = xmls:parse("<html><h1>" + orderid + "</h1></html>");
        messages:setXmlPayload(m, xmlPayload);
        reply m;
    }
}

This constructs an HTML and outputs a reply as below:

<html>
    <h1>123</h1>
</html>

This is still not HTML, rather an HTML constructed as an XML. If you need to render in a webpage, you can extract XML payload directly to be used in your rendering.

Please note that the XML support in Ballerina is under redesign at the moment to provide better native like support. Hence, the above example implementation may change in future.



来源:https://stackoverflow.com/questions/44621416/outputtng-html-by-using-ballerina

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!