Skip to content

Instantly share code, notes, and snippets.

@yupadhyay
Last active December 30, 2017 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save yupadhyay/59bedff99230e9824fc2 to your computer and use it in GitHub Desktop.
Save yupadhyay/59bedff99230e9824fc2 to your computer and use it in GitHub Desktop.
import javax.inject.Inject;
import javax.inject.Named;
import javax.jcr.Session;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
/**
*
* @author Yogesh Upadhyay
*
*/
@Model(adaptables = { SlingHttpServletRequest.class, Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class CustomModel {
// If the field or method name doesn't exactly match the property name, @Named
// can be used:
@Inject
@Named("author")
private String authorPath;
// Can use default value
@Inject
@Default(values = "")
private String bodyText;
// Can inject Custom Sling Object which exactly match sling object name
// http://adobe-consulting-services.github.io/acs-aem-commons/features/aem-sling-models-injectors.html
@SlingObject
private Resource currentResource;
// Can inject custom Object
@Inject
private ResourceResolver resourceResolver;
//Inject a custom osgi service all available injector https://sling.apache.org/documentation/bundles/models.html#available-injectors
@Inject @Source("osgi-services")
private YourService yourService;
// This will return author property from current resource
// same as properties.get("author") from jsp
public String getAuthorPath() {
return this.authorPath;
}
// return body text property
public String getBodytext() {
return this.bodyText;
}
// Example of how you can use sling Injector
public String getResourcePath() {
return currentResource.getPath();
}
// Example of how you can use custom injector
public Session getSession() {
return this.resourceResolver.adaptTo(Session.class);
}
// There are many other cool things you can do with sling model.
// more example http://sling.apache.org/documentation/bundles/models.html
}
@rammohany
Copy link

defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL is giving a compilation error i am using org.apache.sling.models 1.2.0

@yupadhyay
Copy link
Author

@rammohany yes, it is deprecated in Model 1.2.0. You can ignore that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment