Mat*_*zak 1 java spring spring-el java-8
我尝试学习SpEL,但我的一个脚本不起作用.我的脚本是:
changePages(#{T(StoryContent).pages}, (x, y) -> x.replaceAll(y, x.substring(0, 1) + '...' + x.substring(x.length() - 1, x.length())))
Run Code Online (Sandbox Code Playgroud)
Java代码:
public void validate(Story story, List<StoryRule> rules) throws NoSuchMethodException, SecurityException {
StoryContent storyContent = storyContentRepository.findOne(story.getContentId());
story.setStatus(SchedulerStatus.VALIDATION);
storyRepository.save(story);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext inventorContext = new StandardEvaluationContext(storyContent);
inventorContext.registerFunction("changePages", ValidationByRuleServiceImpl.class
.getDeclaredMethod("changePages", new Class[] { List.class, BiFunction.class }));
try {
rules.stream().map(StoryRule::getScript)
.forEach(script -> parser.parseExpression(script).getValue(inventorContext));
if (!storyContent.getTitle().equals(story.getName()))
story.setName(storyContent.getTitle());
story.setStatus(SchedulerStatus.PUBLISHED);
storyContentRepository.save(storyContent);
} catch (Exception e) {
LOG.error(e.getMessage());
story.setStatus(SchedulerStatus.ERROR);
} finally {
storyRepository.save(story);
}
}
private void changePages(List<String> pages, BiFunction<String, String, String> function) {
pages.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Expression [changePages(T(StoryContent).pages, (x, y) -> x.replaceAll(y, x.substring(0, 1) + '...' + x.substring(x.length() - 1, x.length())))] @37: EL1043E: Unexpected token. Expected 'rparen())' but was 'comma(,)'
Run Code Online (Sandbox Code Playgroud)
我从DB获得SpEL脚本列表.
我不知道有什么问题,我的脚本还是我的java代码?
| 归档时间: |
|
| 查看次数: |
2745 次 |
| 最近记录: |