Building an App with the frostillic.us Framework, Part 4
Jul 17, 2014, 3:44 PM
- Building an App with the frostillic.us Framework, Part 1
- Building an App with the frostillic.us Framework, Part 2
- Building an App with the frostillic.us Framework, Part 3
- Building an App with the frostillic.us Framework, Part 4
- Building an App with the frostillic.us Framework, Part 5
- Building an App with the frostillic.us Framework, Part 6
- Building an App with the frostillic.us Framework, Part 7
- Define the data model
- Create the view and add it to an XPage
- Create the editing page
- Add validation and translation to the model
- Add notification to the model
- Add sorting to the view
- Basic servlet
- REST with Angular.js
Add validation and translation to the model
Now that we have a page to edit the model objects, it's time to spruce them up a bit by adding some type information, an additional field, and some basic translation. When we first set up the Note model, it didn't contain any information about the fields it expects, instead leaving everything up to the pass-through defaults. Now, we'll codify the two fields we added last time, add validation to the title, and add a date/time field:
package model; import java.util.Date; import javax.persistence.Table; import org.hibernate.validator.constraints.NotEmpty; import frostillicus.xsp.model.domino.AbstractDominoModel; @Table(name="Note") public class Note extends AbstractDominoModel { private static final long serialVersionUID = 1L; @NotEmpty String title; Date posted; String body; // *snip* }
Model object fields work in the absence of explicit getters and setters - you CAN specify those for additional complexity, but they are not required. You can see here the addition of a validator care of Hibernate Validator. The @NotEmpty
annotation works similarly to "not empty" in EL: it requires that the String not be null and also that it not be blank. We also added a date/time field named "Posted" (date- and time-only fields can be added via the java.sql.Date
and java.sql.Time
classes, respectively) - let's add a form row back on our note.xsp page:
<xe:formRow binding="#{controller.components[note].Title}"/> <xe:formRow binding="#{controller.components[note].Posted}"/> <xe:formRow binding="#{controller.components[note].Body}"> <xe:djTextarea/> </xe:formRow>
I didn't add any specific information to flag "Title" as required or "Posted" as a date/time field on the XPage, but the controller's component binding picks both up:
The controller will add any validation annotations to the control, but it has special support for @NotEmpty
and @NotNull
to also add a required validator and property explicitly. Additionally, since the "Posted" field stores both a date and time, the controller builds a text box with a date/time converter and helper, in the same fashion as setting the data type in the GUI.
Now, though the "Posted" field name matches Domino's idiom from the mail template, it's sort of a weird name to present to a user, so we'll use this as an introduction to translation and creating human-friendly names. The way labeling works in Framework component binding, it looks for a resource bundle named in the form "model_translation.properties". Because it builds on the XSP stack's standard resource-bundle code, it also allows localized versions in the format "model_translation_fr.properties", picking the most appropriate one for the browser. The format of the keys to match the fields is the full class name, then a dot, then the name of the field. So here's a basic "model_translation.properties" File Resource:
model.Note.Posted=Created model.Note.Body=Note Body
By creating this file, the controller automatically picks up on the new human-readable names:
And this goes for other locales as well. Here's a poorly-translated "model_translation_fr.properties" file:
model.Note.Title=Titre model.Note.Posted=Créé model.Note.Body=Corps
And if I switch the browser to use French as its top preferred language (the "Save" button is still the purview of normal XPages translation):
By moving these concerns - data type, validation, and translation - into the data model, it not only significantly lowers the amount of code on the XPage itself, but also makes it much easier to find the canonical rules. Types and validation are always in the model class - not strewn across half a dozen XPages and custom controls - and translations are always in the model translation file and tied directly to the fields they're translating - not hidden in XPage-specific properties files keyed by ID paths.
In the next part, we'll add a bit of UI friendliness to the note creation/editing process using a common messages control and a flashScope object.