AEM Multifield - 修改多字段值中的值

问题描述 投票:0回答:1

多字段问题

NotificationAlert.java

@Model(adaptables = {SlingHttpServletRequest.class,
        Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class NotificationAlerts {

 @Inject
    @Via("resource")
    @Default(values = CommonConstants.EMPTY)
    @Named("notificationItems")
    private List<ListingModel> notificationItems;

    public List<ListingModel> getNotificationItems() {
        return notificationItems;
    }

    public void setNotificationItems(List<ListingModel> notificationItems) {
        this.notificationItems = notificationItems;
    }

@PostConstruct
    private void initModel() {
       for(ListingModel model : notificationItems)
       {
         String createTime = model.getCreateTime();
         String formattedTime = changeDateFormat(createTime); 
//When I debugged I got the formattedTime correctly
//But not setting below
         model.setCreateTime(formattedTime); 
       }
    }

String changeDateFormat(String dateValue)
{
//definiton
}
}

以及 ListingModel.java 接口

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public interface ListingModel {

@Inject
    String getNotificationLogo();
    
    @Inject
    String getCreateTime();

    public void setCreateTime(String value);
    
    @Inject
    boolean isActive();

}

我尝试在界面中声明一个setter函数并尝试在其中调用它 @PostConstruct。

但没有改变

我想使用changeDateFormat(String value)格式化createTime

或者我可以直接调用changeDateFormat()吗?

我该怎么做?

提前致谢

编辑:我想知道如何修改@PostConstruct中编辑的createTime值。设置器没有设置任何东西。

另外,界面中还有一个setter方法

java aem sling aem-6 sling-models
1个回答
0
投票

当从接口而不是类创建 sling 模型时,sling 仅实现用

@Inject
注释的 getter。其他任何内容都会被虚拟的无操作实现删除,这就是为什么 setter 不执行任何操作的原因。

如果您希望 setter 工作,请将

ListingModel
创建为类而不是接口,并注入字段而不是方法。

然后,您可以将自己的 getter 和 setter 添加到

ListingModel
,如果需要,他们可以修改该字段:

@Model(
    adaptables = Resource.class,
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
public class ListingModel {
    @Inject
    private String notificationLogo;
    
    @Inject
    private String createTime;
    
    @Inject
    private boolean active;

    public String getNotificationLogo() {
        return notificationLogo;
    }

    public String getCreateTime() {
        return createTime;
    }

    public String isActive() {
        return active;
    }

    public void setCreateTime(String value) {
        this.createTime = value;
    }
}

如果格式化创建时间的逻辑不依赖于

NotificationAlerts
中的任何内容,您甚至可以在
getCreateTime
方法中即时进行格式化,从而完全不需要设置器或
 
@PostConstruct
上的NotificationAlerts
方法。

© www.soinside.com 2019 - 2024. All rights reserved.