Annotations can be used to create the element declarations and kick in when you declare an element implementation without an ID or file. All that is needed is the @Elem annotation on the element class.
The following conventions are being used by default:
- if no ID is provided as an annotation value, the short class name will be used (without the package)
- if no URL is provided as an annotation value, the lower cased short class name will be used
HelloWorld can thus be rewritten like this:
@Elem
public class HelloWorld extends Element {
public void processElement() {
Template template = getHtmlTemplate("helloworld");
template.setValue("hello", "Hello world.");
print(template);
}
}
With the following site declaration:
<site>
<arrival destid="HelloWorld"/>
<element implementation="HelloWorld"/>
</site>
The element will automatically have the ID HelloWorld and be accessible through the URL "http://localhost:8080/helloworld
".
From here onwards, you can start expanding the element declaration with annotations by adding attributes to the @Elem annotation. You can find detailed information in the Javadocs
.
If you use a flowlink or a datalink with a destClass annotation value (instead of destId), the destination element class should also be annotated with @Elem for its ID to be obtained. RIFE assumes that this element has been declared in the site-structure and its ID will be evaluated inside the scope of the active sub-site. If you changed the ID of the destination element in the site structure, RIFE is not able to find that one since it's possible for an element class to be used several times with different IDs. If you need to reference an annotation element class that's part of another site structure, you can use the destClassIdPrefix annotation attribute.
To reduce the duplication of string literals for exit names, you can use the FlowlinkExitField
and ExitField
annotations. These allow you to annotate public final static String class fields instead of having to specify the exit names in attribute annotations of the main Elem annotation. Afterwards, it's easy to reuse these exit name fields inside your code to trigger the exits. This setup gives you compile-time safety for exit names.
For example:
@Elem
public class ProductListEntry extends Element {
@FlowlinkExitField(destClass = EditProduct.class,
destClassIdPrefix = "^Admin",
datalinks = {
@Datalink(srcOutput="productId", destInput="productId")
})
public static final String EXIT_EDIT_PRODUCT = "editProduct";
@FlowlinkExitField(destClass = ShowProduct.class,
datalinks = {
@Datalink(srcOutput="productId", destInput="productId")
})
public static final String EXIT_SHOW_PRODUCT = "showProduct";
private Product product;
@OutputProperty
public int getProductId() {
if (product != null) return product.getId();
return -1;
}
public void processElement() {
exit(EXIT_SHOW_PRODUCT);
}
}
Instead of declaring inputs, outputs, inbeans, outbeans, incookies, outcookies, submissions, parameters, submission beans and files inside the @Elem annotation, you can reduce name duplication by annotating them directly on setters, getters and submission method handlers (doSubmissionname format) with the InputProperty
, OutputProperty
, InBeanProperty
, OutBeanProperty
, InCookieProperty
, OutCookieProperty
, SubmissionHandler
, ParamProperty
, SubmissionBeanProperty
, and FileProperty annotations
.
Note that for the property declaration of parameters, files, and beans for a submission, RIFE will add them to the last declared submission. However, Java doesn't guarantee the order of methods in a class. That's why you need to use the Priority
annotation to prioritize the order in which the annotated methods will be processed. The priority is not a traditional single integer that has to contain the correct sequential order, but it's an array of integers instead. This allows you to create logic prioritized groups and partition your methods like that. The first element of the array can for example be used to give a number to the submissions, and the second array element can then be used to indicate which methods belong to those submissions.
For example:
@Elem
public class MyElement extends Element {
private String param1;
private String param2;
private String param3;
private UploadedFile file1;
private UploadedFile file2;
@Priority({1})
@SubmissionHandler
public void doMySubmission() {
}
@Priority({1, 1})
@ParamProperty
public void setParam1(String param1) {
this.param1 = param1;
}
@Priority({1, 1})
@FileProperty
public void setFile1(UploadedFile file1) {
this.file1 = file1;
}
@Priority({2})
@SubmissionHandler
public void doAnotherSubmission() {
}
@Priority({2, 1})
@ParamProperty
public void setParam2(String param2) {
this.param2 = param2;
}
@Priority({2, 1})
@ParamProperty
public void setParam3(String param3) {
this.param3 = param3;
}
@Priority({2, 1})
@FileProperty
public void setFile2(UploadedFile file2) {
this.file2 = file2;
}
public void processElement() {
}
}
The code above declares the submission 'mySubmission' and adds the parameter 'param1' and the file 'file1' to it. The second submission is 'anotherSubmission' and it will contain 'param2', 'param3' and 'file2'. You'll notice that the priorities '1,1' and '2,1' are used several times. This is because the order of the parameters and the files doesn't matter, only the first integer that ties them to the submission with the same priority value is important.
It's important to note that any method that doesn't contain a @Priority annotion will be processed before those that are prioritized. This means that you can still use a @Submission declaration inside the @Elem annotation and add parameters and files to it. The submission in the @Elem annotation will be the first that is processed and the non prioritized methods will be added to it since they come before the rest.