Using Model Encoders with a Polymer Template Model

Note
Use Lit templates instead
Lit templates are recommended. Polymer templates are available in the next long term supported Vaadin version (LTS), but they are deprecated.

Using ready-made beans in your model can be challenging if you have no control over their properties (because they are provided as binary class files, for example) or their structure is inappropriate for your template model.

Encoding Property Types

You can use the @Encode annotation to encode any property type to a supported type.

A common use case is a database backend with JPA entities that have identifiers of the Long type. Vaadin does not support the Long type because it cannot be mapped correctly to any JavaScript type.

One solution is to exclude any property of this type using the @Exclude annotation. See Using Beans with a PolymerTemplate Model for an example of how to use this annotation.

Alternatively, you can use the @Encode annotation. This is useful when you need a bean property with this identifier on the client side for any reason, for example to reference it. In these circumstances, the @Encode annotation, encodes the Long value to a String value and sends it to the client side as a string. When the client sends the value back, it is decoded back to a Long.

Example: Defining the Person JPA entity class and using the @Encode annotation in the template model class.

Java

  1. @Entity
  2. public class Person implements Serializable {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6. public Long getId() {
  7. return id;
  8. }
  9. }
  10. public interface MyModel extends TemplateModel {
  11. @Encode(value = LongToStringEncoder.class, path = "id")
  12. void setPerson(Person person);
  13. Person getPerson();
  14. }
  • The @Encode annotation parameter path = "id" is used to address the id sub-property of the person property. By default the path value is "" which means that an encoder is applied to the property itself, in this case the Person.

Example: LongToStringEncoder encoder.

Java

  1. public class LongToStringEncoder implements ModelEncoder<Long, String> {
  2. @Override
  3. public String encode(Long modelValue) {
  4. return Optional.ofNullable(modelValue).map(Object::toString)
  5. .orElse(null);
  6. }
  7. @Override
  8. public Long decode(String presentationValue) {
  9. return Optional.ofNullable(presentationValue).map(Long::valueOf)
  10. .orElse(null);
  11. }
  12. }
  • You can access the id property of the Person bean in your code on the client side in the usual way. Note that it will be of the String type instead of a number.

Using Encoders to Split Property Values

An encoder can also be used to split a single property value into several sub-properties in order to use them in different UI controls. For example, you may want to have 3 input fields (day, month, and year) for a birth date field, instead of one.

Example: Using the @Encode annotation in to define the Date property in a template model.

Java

  1. public interface MyModel extends TemplateModel {
  2. Date getBirthDate();
  3. @Encode(DateToDateBeanEncoder.class)
  4. void setBirthDate(Date birthDate);
  5. }

Example: Defining the DateBean class.

Java

  1. public class DateBean implements Serializable {
  2. private String day;
  3. private String month;
  4. private String year;
  5. public String getDay() {
  6. return day;
  7. }
  8. public void setDay(String day) {
  9. this.day = day;
  10. }
  11. public String getMonth() {
  12. return month;
  13. }
  14. public void setMonth(String month) {
  15. this.month = month;
  16. }
  17. public String getYear() {
  18. return year;
  19. }
  20. public void setYear(String year) {
  21. this.year = year;
  22. }
  23. }

Example: Defining the DateToDateBeanEncoder encoder class.

Java

  1. public class DateToDateBeanEncoder implements ModelEncoder<Date, DateBean> {
  2. @Override
  3. public DateBean encode(Date modelValue) {
  4. if (modelValue == null) {
  5. return null;
  6. }
  7. DateBean bean = new DateBean();
  8. Calendar calendar = GregorianCalendar.getInstance();
  9. calendar.setTime(modelValue);
  10. bean.setDay(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
  11. bean.setMonth(Integer.toString(calendar.get(Calendar.MONTH) + 1));
  12. bean.setYear(Integer.toString(calendar.get(Calendar.YEAR)));
  13. return bean;
  14. }
  15. @Override
  16. public Date decode(DateBean presentationValue) {
  17. if (presentationValue == null) {
  18. return null;
  19. }
  20. int year = Integer.parseInt(presentationValue.getYear());
  21. int day = Integer.parseInt(presentationValue.getDay());
  22. int month = Integer.parseInt(presentationValue.getMonth()) - 1;
  23. Calendar calendar = GregorianCalendar.getInstance();
  24. calendar.set(year, month, day);
  25. return calendar.getTime();
  26. }
  27. }
  • The Date property is encoded to three sub-properties: day, month and year.

Example: Using the sub-properties in a JavaScript Polymer template (snippet only).

js

  1. static get template() {
  2. return html`
  3. <div style="width: 200px;">
  4. <label>Birth date:</label>
  5. <label for="day">Enter your birthday:</label><paper-input id="day" value="{{birthDate.day}}"></paper-input>
  6. <label for="month">Enter the month of your birthday:</label><paper-input id="month" value="{{birthDate.month}}"></paper-input>
  7. <label for="year">Enter the year of your birthday:</label><paper-input id="year" value="{{birthDate.year}}"></paper-input>
  8. <button on-click="commit" id="commit">Commit</button>
  9. </div>`;
  10. }
  • Each of the three sub-properties (day, month, and year) has its own editor. On the server side, it is still one property, birthDate.

  • You need use your original property name (birthDate in this example (not dateBean)) as a prefix to access the sub-properties.