REST

From Documentation
Revision as of 02:57, 18 January 2022 by Hawk (talk | contribs) (correct highlight (via JWB))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Last Update : 2022/01/18


Version Date Content
     



Last Update : 2022/01/18

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.