Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从Spring表达式中修改rootObject_Javascript_Java_Spring_Spring El - Fatal编程技术网

Javascript 从Spring表达式中修改rootObject

Javascript 从Spring表达式中修改rootObject,javascript,java,spring,spring-el,Javascript,Java,Spring,Spring El,是否可以使用SpEL从表达式中修改提供的rootObject 考虑以下代码以了解我的意思: Pojo: 表达方式: Person person = new Person(); person.setAge(18); SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader()); ExpressionParser p

是否可以使用
SpEL
从表达式中修改提供的
rootObject

考虑以下代码以了解我的意思:

Pojo:

表达方式:

Person person = new Person();
person.setAge(18);

SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
ExpressionParser parser = new SpelExpressionParser(config);
Expression ex = parser.parseExpression("age >= 18");
boolean result = ex.getValue(person, Boolean.class);
看看下面我想做什么。可能吗

ex = parser.parseExpression("if (age >= 18) {mature = true}");
// person now has mature == true
编辑:

可以使用支持
JavaScript
并包含在
JVM
中的
javax.script
来代替
SpEL
。这里有一个例子:

ScriptEngineManager manager = new ScriptEngineManager(); 
ScriptEngine jsEngine = manager.getEngineByName("JavaScript");

Person person = new Person();
person.setAge(18);

jsEngine.put("person", person);

jsEngine.eval("if (person.getAge() >= 18) { person.setMature(true); }");

// Calling person.isMature() in Java will now return `true`.

否您将获得下一个异常
SpelParseException
,并显示下一条消息error
解析有效表达式后,表达式中仍有更多数据

您可以通过以下两个选项进行操作:

使用三元运算符: 使用两个spEL表达式:
谢谢你的回答。看起来SpEL并不是为我的需求而设计的。我可能会使用javax.script,正如我在这篇博文中所描述的
ScriptEngineManager manager = new ScriptEngineManager(); 
ScriptEngine jsEngine = manager.getEngineByName("JavaScript");

Person person = new Person();
person.setAge(18);

jsEngine.put("person", person);

jsEngine.eval("if (person.getAge() >= 18) { person.setMature(true); }");

// Calling person.isMature() in Java will now return `true`.
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
ExpressionParser parser = new SpelExpressionParser(config);

Boolean parsedValue = parser.parseExpression("age >= 18 ? Mature=true : Mature=false").getValue(person, Boolean.class);

System.out.println(person.getMature()); // Output = true
System.out.println(parsedValue); //Output = true
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
ExpressionParser parser = new SpelExpressionParser(config);
Expression ex = parser.parseExpression("age >= 18");
boolean result = ex.getValue(person, Boolean.class);

if(result)
   parser.parseExpression("mature").setValue(person, "true");

System.out.println(person.getMature()); //Output = true