本文介绍了如何创建用于 Thymeleaf 和 Spring 的新表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


有人知道如何在 thymeleaf 中扩展表达式吗?(我需要 thymeleaf 中的variety"功能).
克隆数字"表达式有效,但是...
当我自己做表情时,我有错误:


Anyone know how to extend an expressions in thymeleaf? (I need a "variety" function in thymeleaf).
Cloning a "Numbers" expression works, but...
When I make my own expression, I have error:

严重:servlet [appServlet] 的 Servlet.service() 在路径 [] 的上下文中抛出异常

SEVERE: Servlet.service() for servlet [appServlet] in context with path [] threw exception

  [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

有什么建议吗?

编辑(我的代码):包 org.springframework.expression;

Edit (my code): package org.springframework.expression;

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 < 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 >= 10 && number % 100 <= 20)
            return varietyForOthers;
        if(in_array(number%10, new int[] {2,3,4}))
            return varietyFor234;
        return varietyForOthers;
    }
}

编辑(忘记了,像往常一样):我想像这样使用它: ${utils.Variety(...)}

Edit (forgot, like allways):I would like to use it like: ${utils.Variety(...)}

推荐答案

以下异常说明上下文中没有 Variety 对象.

The following exception shows that there is no Variety object on the context.

Attempted to call method  Variety(java.lang.Integer,java.lang.String,java.lang.String,java.lang.String) on null context object

表达式对象必须通过方言添加.如果您已将方言添加到模板引擎中,您将能够使用 #utils.variety(...).

The expression object must be added via a dialect. If you have added the dialect to your template engine, you will be able to use #utils.variety(...).

public class UtilsDialect implements IExpressionEnhancingDialect {

    public static final String UTILS_EXPRESSION_OBJECT_NAME = "utils";

    private final Variety utils;

    public UtilsDialect (final Variety utils){
        this.utils = utils;
    }

    @Override
    public Map<String, Object> getAdditionalExpressionObjects(final IProcessingContext processingContext) {
        final Map<String, Object> objects = new HashMap<>();
        objects.put(UTILS_EXPRESSION_OBJECT_NAME, variety);
        return objects;
    }
}

还要确保您在混合大小写中编写方法:

Also make sure you write your methods in mixed case:

public abstract class Variety extends StandardEvaluationContext {

    private boolean inArray(...){
        ...
    }

    public String variety(...){
        ...      
    }
}

这篇关于如何创建用于 Thymeleaf 和 Spring 的新表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 17:20