Spring SPEL集合投影

huangapple go评论73阅读模式
英文:

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(&quot;Nikola Tesla&quot;, &quot;Serbian&quot;);
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
context.setVariable(&quot;selectedNationality &quot;, &quot;Serbian&quot;);

New Selection

The new selection SPEL would look like this with #selectedNationality

List&lt;Inventor&gt; list = (List&lt;Inventor&gt;) parser.parseExpression(
        &quot;Members.?[Nationality == #selectedNationality]&quot;).getValue(societyContext);

Works like a charm !

huangapple
  • 本文由 发表于 2020年8月5日 18:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63263203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定