Sling Models are annotation driven Java “POJOs” (Plain Old Java Objects) that facilitate the mapping of data from the JCR to Java variables. With hands on, will explore Sling model.
To get the Dialog value in sling model java file, use @ValueMapValue annotation.
Different annotations that we can use in sling models are mentioned below.
@Model(adaptables = {Resource.class,SlingHttpServletRequest.class},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class HelloWorldModel {
@SlingObject
private Resource currentResource;
@SlingObject
private ResourceResolver resourceResolver;
@ValueMapValue
@Named("myText")
private String textVal;
@OSGiService
private TestService testService; // This is custom service class
@OSGiService
protected ModelFactory modelFactory;
@SlingObject
private SlingHttpServletRequest slingHttpServletRequest;
@ScriptVariable
private Page currentPage;
@ScriptVariable
private PageManager pageManager;
@ScriptVariable
private Style currentStyle;
private final Logger logger = LoggerFactory.getLogger(getClass());
private String message;
@PostConstruct
protected void init() {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
String currentPagePath = Optional.ofNullable(pageManager)
.map(pm -> pm.getContainingPage(currentResource))
.map(Page::getPath).orElse("");
message = "Hello World!\n"
+ "textVal is: " + textVal
+ "Current page is: " + currentPagePath + "\n";
logger.debug("currentPage path="+currentPage.getPath());
}
public String getMessage() {
return message;
}
}
Note: Try to avoid generic annotation: @Inject. Instead, use the specific annotations, like: @ValueMapValue, @OSGiService etc.
You can refer this project from the github repository link.