[英]Sling AEM / Model / Retrieve item text not value
I have an AEM site. 我有一个AEM网站。 My front-end content.xml has a select list of different color options to pick from: 我的前端content.xml有一个可供选择的不同颜色选项的列表:
<items jcr:primaryType="nt:unstructured">
<colors
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
fieldLabel="Select a Color"
name="./colors">
<items jcr:primaryType="nt:unstructured">
<blue
jcr:primaryType="nt:unstructured"
text="Blue"
value="bl blue"/>
<green
jcr:primaryType="nt:unstructured"
text="Green"
value="gr green"/>....
My model looks something like: 我的模型如下所示:
@Model(adaptables=Resource.class)
public class Color{
@Inject @Named("colors") @Optional
private String cssClass ;
@Inject @Named("colors.text") @Optional //This is not working
private String label;
public String getCssClass() {
return cssClass;
}
public String getLabel() {
return label;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
public void setLabel(String label) {
this.label = label;
}
}
This code will return the cssClass string as either "bl blue" or "gr green" depending on what the user selected. 此代码将根据用户选择的内容将cssClass字符串返回为“ bl blue”或“ gr green”。
My question is how do I get the label string to return "Blue" or "Green" (aka the text attribute of the selected color item)? 我的问题是如何获取标签字符串以返回“蓝色”或“绿色”(又称所选颜色项目的文本属性)?
Thanks! 谢谢!
Per @rakhi4110: 每个@ rakhi4110:
It is not possible to get the label, as only the value of the dropdown gets saved in CRX when the author selects a color. 无法获得标签,因为当作者选择颜色时,仅下拉列表的值保存在CRX中。
While what you are asking is not possible, because the value is the only thing that will be stored in JCR (as @rakhi4110 mentioned), you could still add the text value in the value attribute and parse it in the sling model. 虽然您所要询问的内容是不可能的,但是因为值是唯一将存储在JCR中的内容(如@ rakhi4110所述),您仍然可以在value属性中添加文本值并在sling模型中对其进行解析。
Here is one way you can do it: 这是您可以执行的一种方法:
<items jcr:primaryType="nt:unstructured">
<colors
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/select"
fieldLabel="Select a Color"
name="./colors">
<items jcr:primaryType="nt:unstructured">
<blue
jcr:primaryType="nt:unstructured"
text="Blue"
value="Blue:bl blue"/>
<green
jcr:primaryType="nt:unstructured"
text="Green"
value="Green:gr green"/>....
Notice value="Blue:bl blue"
and value="Green:gr green"
注意value="Blue:bl blue"
和value="Green:gr green"
Now in your sling model, you can do: 现在,在吊索模型中,您可以执行以下操作:
@Model(adaptables=Resource.class)
public class Color{
@Inject @Named("colors") @Optional
private String colorValue ;
private cssClass;
private label;
@PostConstruct
protected void init() {
// This is a very rudimentary way to illustrate the point
// you can do this in many other ways/data structures to get the same result
String[] parts = colorValue.split(":");
label = parts[0];
cssClass = parts[1];
}
public String getCssClass() {
return cssClass;
}
public String getLabel() {
return label;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.