How to access Enum fields in a playframework template

I have an Enum that has certain fields that I need to get in my play1.2.4 template

 public Enum WORKER{ FARMER,SMITH,GARDENER } 

Suppose I need to check if the variable "person" in the template is a farmer, a blacksmith or a gardener, how can I do this?

 #{if person.Type==WORKER.FARMER} ...do something... #{/if} 

Here i get

 NullPointerException : Cannot get property 'FARMER' on null object. 

So, the template does not know about Enum WORKER. Since a new instance cannot be created for Enum, how do I make Enum available to the template?

+6
source share
1 answer

Use the name of the absolute class enum in the template. For instance. if your WORKER enumeration is in the model.myenums package, the template code will look like this:

 #{if person.Type == model.myenums.WORKER.FARMER} ...do something... #{/if} 
+10
source

Source: https://habr.com/ru/post/909757/


All Articles