-1

MVEL is not evaluating the expression properly which contains the string with number like below

<div><br>@{11412Test}</div>

Caused by: [Error: invalid number literal: 11412Test] [Near : {... v>
@{11412Test} ....}]

[Line: 1, Column: 57] at org.mvel2.util.ParseTools.isNumber(ParseTools.java:1903) at org.mvel2.ast.ASTNode.setName(ASTNode.java:257) at org.mvel2.ast.ASTNode.(ASTNode.java:422) at org.mvel2.compiler.AbstractParser.createPropertyToken(AbstractParser.java:1428) at org.mvel2.compiler.AbstractParser.nextToken(AbstractParser.java:896) at org.mvel2.compiler.ExpressionCompiler._compile(ExpressionCompiler.java:127) at org.mvel2.MVEL.compileExpression(MVEL.java:832) at org.mvel2.templates.res.CompiledExpressionNode.(CompiledExpressionNode.java:41) at org.mvel2.templates.TemplateCompiler.compileFrom(TemplateCompiler.java:211) ... 131 more

and i am using mvel2-2.4.14-Final version. Please let me know how to resolve this issue. Note: If it is only string'Test' or only number '11412' then it is working

moh
  • 1,426
  • 2
  • 16
  • 43
  • 1
    So what is it you actually want to achieve? The whole string is not a number, so it can't be parsed as one. And from your comment on the answer, it looks like you don't just want a String either. – Jorn Aug 10 '23 at 06:40
  • hi @Jorn, i want to evaluate this expression and replace with the actual value.

    @{11412Test}
    will get replace with

    Hi,some content from hmap
    and Hi,some content from hmap is the actual value of @{11412Test}. which should get replace with the content.
    – moh Aug 10 '23 at 18:53
  • 2
    *"Please provide a proper solution"* The proper solution is to not use names that start with a digit. – Olivier Aug 11 '23 at 19:07

1 Answers1

0

The claim that "it is working with number '11412'" is not true:

import java.util.HashMap;
import org.mvel2.templates.TemplateRuntime;

public class Test
{
    public static void main(String[] args)
    {
        String template = "Hello, @{11412}";
        HashMap<String,Object> vars = new HashMap<String,Object>();
        vars.put("11412", "world");
        System.out.println(TemplateRuntime.eval(template, vars));
    }
}

Output:

Hello, 11412

@{11412} was not replaced by world. 11412 was just interpreted as a literal integer (not as a variable name).

Like in most (if not all) languages, names are not allowed to start with a digit. A token that starts with a digit is interpreted as a number. You may, however, start with an underscore:

import java.util.HashMap;
import org.mvel2.templates.TemplateRuntime;

public class Test
{
    public static void main(String[] args)
    {
        String template = "Hello, @{_11412}";
        HashMap<String,Object> vars = new HashMap<String,Object>();
        vars.put("_11412", "world");
        System.out.println(TemplateRuntime.eval(template, vars));
    }
}

Output:

Hello, world

_11412Test will work as well.

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • I already tried this option but did not work out. How would this string will get replace in actual template. This is not working – moh Aug 10 '23 at 03:21
  • @moh Now that I understand your question, I rewrote my answer. – Olivier Aug 12 '23 at 07:19