【问题标题】:Sling AEM / Model / Retrieve item text not value吊索 AEM / 模型 / 检索项目文本不是值
【发布时间】:2019-01-31 23:27:25
【问题描述】:

我有一个 AEM 站点。我的前端 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"/>....

我的模型看起来像:

@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;
    }
}

此代码将 cssClass 字符串返回为“bl blue”或“gr green”,具体取决于用户选择的内容。

我的问题是如何让标签字符串返回“蓝色”或“绿色”(也就是所选颜色项的文本属性)?

谢谢!

【问题讨论】:

  • 无法获取标签,因为当作者选择颜色时,只有下拉列表的值会保存在 CRX 中。标签仅在渲染对话框时使用,而不是实际保存值的一部分。您可能需要找到一种方法来在选择该选项时保存标签,或者创建标签地图并使用选定的颜色键来查找相应的标签。
  • @rakhi4110 谢谢 - 您节省了大量浪费的时间!

标签: java aem sling sling-models


【解决方案1】:

根据@rakhi4110:

无法获取标签,因为当作者选择颜色时,只有下拉列表的值会保存在 CRX 中。

【讨论】:

    【解决方案2】:

    虽然您的要求是不可能的,因为值是唯一将存储在 JCR 中的东西(如@rakhi4110 提到的),您仍然可以在 value 属性中添加文本值并在 sling 模型中解析它.

    这是您可以做到的一种方法:

    <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"/>....
    

    通知value="Blue:bl blue"value="Green:gr green"

    现在在您的吊索模型中,您可以:

    @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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 2019-02-08
      • 2014-09-28
      • 2022-10-13
      • 1970-01-01
      • 2019-02-22
      • 2017-02-20
      相关资源
      最近更新 更多