Madvoc supports RESTfull URLs. Parts of mapped action path may contain macros - text chunk surrounded with ${ and }. Macros are used to resolve values from request action path. Values are injected in the action object, similarly as request parameters and attributes gets injected.
Here is a simple action class
@MadvocAction
public class RestAction {
Long id;
@Action("/user/${id}")
public void viewUser() {
}
}It's quite obvious: above action method is registered to set of action paths that starts with '/user/'. For example, if request action path is '/user/173' Madvoc will match it to above action method. Second part of the request
will be recognized as macros value. This value will be injected into the action object using the macro name, 'id'.
@MadvocAction
public class RestAction {
Long id;
@Action("/user-${id}.jpg")
public void viewUserImage() {
}
}In this example, Madvoc will match whole set of request action paths: '/user-*.jpg' to this action method. For example, request: /user-173.jpg would be served by this method.
In all above examples, @Action define absolute action path. This is not required, i.e. action path with macros is built as usual, considering package, class and method annotations. However, only @Action annotation may contain macros.
Action path is split in chunks on '/'. Each chunk may contains one and only one macro definition.
In above examples there are no checking if the macro value is a number - so, even invalid action paths (e.g. /user/huh) will be invoked by mapped action methods.
Madvoc provides regular expression matching of the macro values. RegExp pattern can be defined after the macro name, as in following example:
@MadvocAction
public class RestAction {
Long id;
@Action("/user/${id:^[0-9]+}")
public void viewUser() {
}
}This action method is mapped on all request action paths that starts with /user/ and have a number appended as a macro value. Invalid urls will be simply ignored and error 404 will be raised.
Result path is builded from the action path. When action path contains RegExp pattern, resulting path may not be a valid file name (for dispatching to, for example). For such situation we can use replacements in the result, as in following example:
@Action("/user/${id:^[0-9]+}")
public String viewUser() {
return "#[method].ok";
}
Let's analyze the result value. First character is '#', that means 'go back', i.e. strip action method part from the end. Then we are adding a replacement '[method]', that will be replaced with the real method name. So, the result path of the above action method is: '/user/viewUser.ok'.