Question

I have an Enum which has certain fields ,which I need to access in my play1.2.4 template

public Enum WORKER{
   FARMER,SMITH,GARDENER
}

Suppose I need to check if a 'person' variable in the template is a farmer,smith or gardener ,how can I do it?

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

Here I get

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

So,the template doesn't know about the Enum WORKER.Since a new instance cannot be created for Enum ,how should I make the Enum available to the template?

Was it helpful?

Solution

Use the enum's absolute class name in the template. E.g. if your enum WORKER is in the package model.myenums, the template code would look like this:

#{if person.Type == model.myenums.WORKER.FARMER}
   ...do something...
#{/if}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top