英文:
Spring SPEL collection projection
问题
以下是您要翻译的部分:
"What i need is the selection 'Serbian' to come from outside and not be a fixed hard coded String."
"Just for arguments sake consider that, we could get it as 'selectedNationality' from the same society class from the same page in the link."
"Modified class with selectedNationality"
"New Selection"
"When we try that the error is that 'selectedNationality' is not a part of the Member object."
"Does that mean that for collection selection in spring expression language we would need a hard coded String ? If yes does anyone know why ?"
英文:
A simple question on SPEL collection selection.
Look at section 10.5.17 Collection Selection on this page
https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html
List<Inventor> list = (List<Inventor>) parser.parseExpression(
"Members.?[Nationality == 'Serbian']").getValue(societyContext);
What i need is the selection 'Serbian' to come from outside and not be a fixed hard coded String.
Just for arguments sake consider that, we could get it as "selectedNationality" from the same society class from the same page in the link.
Modified class with selectedNationality
public class Society {
private String name;
public static String Advisors = "advisors";
public static String President = "president";
private List<Inventor> members = new ArrayList<Inventor>();
private Map officers = new HashMap();
// new selector field
private String selectedNationality;
......
}
New Selection
The new selection SPEL would look like
List<Inventor> list = (List<Inventor>) parser.parseExpression(
"Members.?[Nationality == selectedNationality]").getValue(societyContext);
When we try that the error is that "selectedNationality" is not a part of the Member object.
Does that mean that for collection selection in spring expression language we would need a hard coded String ? If yes does anyone know why ?
答案1
得分: 0
找到如何做到这一点。所以方法是使用变量,请参阅10.5.11节中的变量https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html
设置变量
在我们的情况下,我们将这样设置变量:
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
context.setVariable("selectedNationality", "Serbian");
新选择
新的选择SPEL将如下所示,带有**#selectedNationality**
List<Inventor> list = (List<Inventor>) parser.parseExpression(
"Members.?[Nationality == #selectedNationality]").getValue(societyContext);
运行得非常顺利!
英文:
Found out how to do it. So the way is to use variables
see 10.5.11 Variables @ https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html
Set Variable
So in our case we wold do this set variable :
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
context.setVariable("selectedNationality ", "Serbian");
New Selection
The new selection SPEL would look like this with #selectedNationality
List<Inventor> list = (List<Inventor>) parser.parseExpression(
"Members.?[Nationality == #selectedNationality]").getValue(societyContext);
Works like a charm !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论