1.创建Sling Model
@Model:声明一个bean为Sling模型。
@Inject:将资源属性注入类变量中。
@PostConstruct:声明根据业务逻辑派生信息后初始化bean的函数。在所有注入完成后调用该函数。
@Model(adaptables = Resource.class)
public class ContactUsModel {
private String mailAddress;
private String phoneNumber;
@Inject
protected String title;
@Inject
protected String officeCode;
@PostConstruct
protected void init() {
// Business logic to transform injected values and assign to bean variables
// Example: Resolve mailAddress/phoneNumber applicable for the user's region, based on officeCode
}
public String getMailAddress(){
return mailAddress;
}
public String getPhoneNumber(){
return phoneNumber;
}
}
在上面的例子中,adaptables = Resource。类的作用是:将sling模型映射到sling资源上。
@Inject会将资源中的“contacts”属性注入“contacts”类变量中。属性值注入发生在将资源调整为值映射之后。
2.Optional/Required fields
@Model注释提供了’ defaultInjectionStrategy ‘属性,指示sling模型中的注入字段是否应该是必需的/可选的。
Injection strategy on class level:
使用’ defaultInjectionStrategy = defaultInjectionStrategy。将所有注入的字段标记为可选
使用’ defaultInjectionStrategy = defaultInjectionStrategy。将所有注入字段标记为REQUIRED。如果没有指定’ defaultInjectionStrategy ',它也是默认配置。
Injection strategy on Field level:
可以使用@Required / @Optional注释有选择地将字段标记为required/optional。
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ContactUsModel {
private String mailAddress;
private String phoneNumber;
@Inject
@Required
protected String title;
@Inject
protected String officeCode;
@PostConstruct
protected void init() {
// Business logic to transform the Injected values and assign to bean Variables
// Example: Resolve mailAddress/phoneNumber applicable for the user's region, based on officeCode
}
public String getMailAddress(){
return mailAddress;
}
public String getPhoneNumber(){
return phoneNumber;
}
}
3.Adapt Model from Resource / SlingHttpServletRequest
一个模型可以适应资源/ SlingHttpServletRequest。这取决于需要注入的字段。
如果只需要资源属性,最好使用:
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
如果Sling对象(如。还需要currentStyle, currentPage, xssApi等),更适应SlingHttpServletRequest。
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
4.Default Value
@Default允许为字段(包括数组)指定默认值。
@Inject @Default(values="Contact Us")
private String title;
@Inject @Default(intValues={1,2,3,4})
private int[] integers;
5.Alternate field name
Use @Named if the field name is different from the property name
@Inject @Named("jcr:created")
private String created;
6.Via
基于JavaBean属性注入自适应的字段。使用“SlingHttpServletRequest”从资源中注入属性值。
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ContactUsModel {
@Inject @Via("resource")
private String title;
}
7.Self
向字段注入可适应的对象。
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ContactUsModel {
//Accessing the resource that is adapted to ContactusModel
@Self
protected Resource resource;
}
https://techrevel.blog/2017/03/18/sling-model-annotations/