title: How can I create a new expression for use with Thymeleaf and Spring?
body: <p><br/>
  Anyone know how to extend an expressions in thymeleaf? (I need a "variety" function in thymeleaf).<br/>
  Cloning a "Numbers" expression works, but...<br/>
  When I make my own expression, I have error:</p>
  
  <p>SEVERE: Servlet.service() for servlet [appServlet] in context with path [] threw exception </p>
  
  <pre><code>  [Request processing failed;
     nested exception is   org.thymeleaf.exceptions.TemplateProcessingException: 
     Exception evaluating SpringEL expression:
     "Variety.Variety(review.usefulScore, 'osoba', 'osoby', 'osób')"
     (static:/cms/fragments/reviews:49)] with root cause
     org.springframework.expression.spel.SpelEvaluationException:
     EL1011E:(pos 8): Method call: 
    Attempted to call method  Variety(java.lang.Integer,java.lang.String,java.lang.String,java.lang.String) on null context object
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Edit (my code):
      package org.springframework.expression;</p>
  
  <pre><code>import org.springframework.expression.spel.support.StandardEvaluationContext;
  
  public abstract class Variety extends StandardEvaluationContext {
  
      private boolean in_array(int needle, int[] haystack) {
  
          for(int i = 0; i &lt; haystack.length; i++) {
              if(haystack[i] == needle) return true;
          }
          return false;
      }
  
      public String Variety(int number, String varietyFor1, String varietyFor234,  String varietyForOthers) {
          if(number == 1)
              return varietyFor1;
          if(number % 100 &gt;= 10 &amp;&amp; number % 100 &lt;= 20)
              return varietyForOthers;
          if(in_array(number%10, new int[] {2,3,4}))
              return varietyFor234;
          return varietyForOthers;
      }
  }
  </code></pre>
  
  <p>Edit (forgot, like allways):
  I would like to use it like: ${utils.Variety(...)}</p>