使用 Spring El 表达式进行模板化
Templating using Spring El expression
我想使用 spring el 进行简单的模板化,例如 "Some text: #{some dynamic value from map}"
。就我而言,docs 中的示例不适合,因为它仅适用于值形式映射:
Map<String, Object> data = new HashMap<String, Object>();
data.put("property", 123);
String message = "#data['property']";
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("data", data);
System.out.println(parser.parseExpression(message).getValue(context, String.class));
在这种情况下输出是 123
,但是 String message = "Some text: #data['property']";
创建了异常
org.springframework.expression.spel.SpelParseException: EL1041E:(pos 5): After parsing a valid expression, there is still more data in the expression: 'text'
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:129)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:60)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:32)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:76)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:62)
我的错误是什么?
文字必须是文字。
您现在拥有的内容在 java...
中看起来像这样
String s = some text: data.get("property");
...这显然无法编译。
如果您在 java 中编写它,则需要使用...
String s = "some text: " + data.get("property");
...因此您需要在 SpEL 中使用等效项...
"'Some text: ' + #data['property']"
作为替代方案,您可能想使用 Expression templating 中提到的 TemplateParserContext
。您的示例将如下所示:
Map<String, Object> data = new HashMap<String, Object>();
data.put("property", 123);
String message = "Some text: #{#data['property']}";
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(message, new TemplateParserContext());
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("data", data);
System.out.println(expression.getValue(context,String.class));
我想使用 spring el 进行简单的模板化,例如 "Some text: #{some dynamic value from map}"
。就我而言,docs 中的示例不适合,因为它仅适用于值形式映射:
Map<String, Object> data = new HashMap<String, Object>();
data.put("property", 123);
String message = "#data['property']";
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("data", data);
System.out.println(parser.parseExpression(message).getValue(context, String.class));
在这种情况下输出是 123
,但是 String message = "Some text: #data['property']";
创建了异常
org.springframework.expression.spel.SpelParseException: EL1041E:(pos 5): After parsing a valid expression, there is still more data in the expression: 'text'
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:129)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:60)
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:32)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:76)
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:62)
我的错误是什么?
文字必须是文字。
您现在拥有的内容在 java...
中看起来像这样String s = some text: data.get("property");
...这显然无法编译。
如果您在 java 中编写它,则需要使用...
String s = "some text: " + data.get("property");
...因此您需要在 SpEL 中使用等效项...
"'Some text: ' + #data['property']"
作为替代方案,您可能想使用 Expression templating 中提到的 TemplateParserContext
。您的示例将如下所示:
Map<String, Object> data = new HashMap<String, Object>();
data.put("property", 123);
String message = "Some text: #{#data['property']}";
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(message, new TemplateParserContext());
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("data", data);
System.out.println(expression.getValue(context,String.class));