WEB*_*TER 1 java spring thymeleaf
有人知道如何在百里香中扩展表达吗?(我在百里香中需要一个“品种”功能)。
克隆“数字”表达式是可行的,但是...
当我创建自己的表达式时,出现错误:
严重:路径为[]的Servlet [appServlet]的Servlet.service()抛出异常
[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
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
编辑(我的代码):包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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑(忘记了,总是如此):我想像这样使用它:$ {utils.Variety(...)}
以下异常表明Variety上下文中没有对象。
Attempted to call method Variety(java.lang.Integer,java.lang.String,java.lang.String,java.lang.String) on null context object
Run Code Online (Sandbox Code Playgroud)
表达式对象必须通过方言添加。如果已将方言添加到模板引擎中,则可以使用#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;
}
}
Run Code Online (Sandbox Code Playgroud)
还要确保以混合大小写方式编写方法:
public abstract class Variety extends StandardEvaluationContext {
private boolean inArray(...){
...
}
public String variety(...){
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3269 次 |
| 最近记录: |