Wiki源代码使用 XWiki 编写一个 add 接口
由用户 Qiongpan Ke 在 2023-07-08 保存的版本 1.1
隐藏最后作者
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | {{groovy}} |
2 | import org.xwiki.velocity.tools.JSONTool; | ||
3 | |||
4 | def a = request.getParameter("a"); | ||
5 | def b = request.getParameter("b"); | ||
6 | if (a && b) { | ||
7 | def result = Integer.valueOf(a) + Integer.valueOf(b); | ||
8 | def json = new JSONTool().serialize(Collections.singletonMap("result", result)); | ||
9 | response.setContentType("application/json"); | ||
10 | def outputStream = response.getOutputStream(); | ||
11 | outputStream.println(json); | ||
12 | outputStream.close(); | ||
13 | } | ||
14 | {{/groovy}} | ||
15 | |||
16 | 在浏览器中访问本页面时,可以看到此信息。想要完成计算,可以对本页面地址发起 HTTP-POST 请求,例如: | ||
17 | |||
18 | {{code language="sh"}} | ||
19 | curl -fsSL --form 'a=123' --form 'b=456' https://www.keqiongpan.cn/bin/technology/tools/xwiki/add | jq . | ||
20 | {{/code}} | ||
21 | |||
22 | 执行结果如下: | ||
23 | |||
24 | {{code language="json"}} | ||
25 | { | ||
26 | "result": 579 | ||
27 | } | ||
28 | {{/code}} | ||
29 | |||
30 | 或者可通过如下 HTML 表单,即时发起计算请求: | ||
31 | |||
32 | {{example}} | ||
33 | {{html}} | ||
34 | <form action="" method="POST"> | ||
35 | <p>a = <input type="text" name="a" value="123" /></p> | ||
36 | <p>b = <input type="text" name="b" value="456" /></p> | ||
37 | <p><input type="submit" value="计算 a + b 的结果" /></p> | ||
38 | </form> | ||
39 | {{/html}} | ||
40 | {{/example}} |