REST"
From Documentation
m |
m (correct highlight (via JWB)) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 5: | Line 5: | ||
For example, we could implement a method to return the method's name to execute based on HTTP methods as follows. | For example, we could implement a method to return the method's name to execute based on HTTP methods as follows. | ||
− | <source lang=" | + | <source lang="java"> |
//Extract from zest-samples.war | //Extract from zest-samples.war | ||
package org.zkoss.zest.examples.rest; | package org.zkoss.zest.examples.rest; | ||
Line 36: | Line 36: | ||
</source> | </source> | ||
− | Then, we map the HTTP method to action's method in < | + | Then, we map the HTTP method to action's method in <code>WEB-INF/zest.xml</code>: |
<source lang="xml"> | <source lang="xml"> | ||
Line 50: | Line 50: | ||
</source> | </source> | ||
− | where [[ZEST Essentials/Configuration/zest.xml/xel-method|the xel-method element]] is used to declare a static method as an EL function (<code> | + | where [[ZEST Essentials/Configuration/zest.xml/xel-method|the xel-method element]] is used to declare a static method as an EL function (<code>toMethodName</code> in the above example). |
=Version History= | =Version History= |
Latest revision as of 02:57, 18 January 2022
Under REST (Representational State Transfer) architectural pattern, the operations are usually based on HTTP methods. It can be done straightforwardly in ZEST by use of EL expressions.
For example, we could implement a method to return the method's name to execute based on HTTP methods as follows.
//Extract from zest-samples.war
package org.zkoss.zest.examples.rest;
public class RESTfulAction {
public String get() { //the get operation
...
}
public String add() { //the add operation
...
}
public String delete() { //the delete operation
...
}
public String modify() { //the modify operation
...
}
/** Converts HTTP methods to this action's method. */
public static String toMethodName(String requestMethod) {
requestMethod = _methods.get(requestMethod.toLowerCase());
return requestMethod != null ? requestMethod: "get";
}
private static final Map<String, String> _methods = new HashMap<String, String>();
static {
_methods.put("delete", "delete");
_methods.put("post", "add");
_methods.put("put", "modify");
}
}
Then, we map the HTTP method to action's method in WEB-INF/zest.xml
:
<zest>
<xel-method prefix="c" name="toMethodName"
class="org.zkoss.zest.examples.rest.RESTfulAction"
signature="java.lang.String toMethodName(java.lang.String)"/>
<action path="/rest" method="${c:toMethodName(request.method)}"
class="org.zkoss.zest.examples.rest.RESTfulAction">
<result>/WEB-INF/rest/RESTfulView.jsp</result>
</action>
</zest>
where the xel-method element is used to declare a static method as an EL function (toMethodName
in the above example).
Version History
Version | Date | Content |
---|---|---|