Sling Models
Resource
@Model(adaptables=Resource.class)
public class NavigationModel{
@self
private Resource resource;
@Inject
public NavigationModel(@Named("resource") @Optional Resource resource){
this.resource=resource;
}
}
SlingHttpServletRequest
@Model(adaptables={SlingHttpServletRequest.class})
public class RequestModel{
private Resource resource;
@Inject
SlingHttpServletRequest request;
@PostConstruct
protected void init(){
resource=request.getResource();
}
}
Setting Property Using ResouceResolver and Resource
//Sets Property to a Resource
@Inject
private SlingHttpServletRequest request;
protected void setProperty(final Resource resource,final String name,final int value){
try{
final ResourceReolver resourceResolver=request.getResourceResolver();
final ValueMap properties=resouce.adapTo(ModifiableValueMap.class);
properties.put(name,value);
resourceResolver.refresh();
resourceResolver.commmit();
}catch(PersistenceException pe)
{
Logger.error("unable to {} update property [{}]",name,resouce.getName(),pe);
}
}
Resource
@Model(adaptables=Resource.class)
public class NavigationModel{
@self
private Resource resource;
@Inject
public NavigationModel(@Named("resource") @Optional Resource resource){
this.resource=resource;
}
}
SlingHttpServletRequest
@Model(adaptables={SlingHttpServletRequest.class})
public class RequestModel{
private Resource resource;
@Inject
SlingHttpServletRequest request;
@PostConstruct
protected void init(){
resource=request.getResource();
}
}
Setting Property Using ResouceResolver and Resource
//Sets Property to a Resource
@Inject
private SlingHttpServletRequest request;
protected void setProperty(final Resource resource,final String name,final int value){
try{
final ResourceReolver resourceResolver=request.getResourceResolver();
final ValueMap properties=resouce.adapTo(ModifiableValueMap.class);
properties.put(name,value);
resourceResolver.refresh();
resourceResolver.commmit();
}catch(PersistenceException pe)
{
Logger.error("unable to {} update property [{}]",name,resouce.getName(),pe);
}
}
Good one..really helpful..
ReplyDeleteThis comment has been removed by the author.
ReplyDelete