Sling Model are excellent way to Map your resources to Class or Interface. Many of us has worked in ORM frameworks (Hibernate, Ibatis...) ,where we maintain a direct mapping between the data fields and plain old java object class either through annotation or through configuration file.We can achieve the same functionality where we can map resources directly POJO/Interface using Sling Model capability, It is purely annotation driven and creating a Sling Model is quite simple , we need to add @Model class level annotation , with adaptable type specified.The annotation can be applied to either class or interface.
Each field or method to be used in injected by Sling Model are annotated with @Inject annotations.
As there support for both Interface and Class , we can use it as per the requirement ,mostly interface based sling model would suffice with less code.
How to pass parameters to Sling Model
Sling Model offers the flexibility to passing parameter, the passed parameter will be available as request attributes to the model.Assuming the following markup
The model would retrieve the parameter using following constructs.
Now the Question is Class or Interface
Since you can use either a Class or Interface for Sling Models, one question you may ask is, which should I use?
In most cases, using a Sling Models Interface fill require less code to accomplish the same task. The most common case where you would want to use a Class is if you need to do any filtering or manipulation of the values being returned. Using a Class for your model, you can then inject the values into the field and generate the needed return value
The following implementation to ascertain the above statement , where we can display the value of message variable as per our need.
A number of additional annotations can be used to modify the behavior of the Class , please refer the Sling Model documentation.
Please leave any comments or feed back ,if you have any question or run into any issues
Each field or method to be used in injected by Sling Model are annotated with @Inject annotations.
As there support for both Interface and Class , we can use it as per the requirement ,mostly interface based sling model would suffice with less code.
How to pass parameters to Sling Model
Sling Model offers the flexibility to passing parameter, the passed parameter will be available as request attributes to the model.Assuming the following markup
The model would retrieve the parameter using following constructs.
Now the Question is Class or Interface
Since you can use either a Class or Interface for Sling Models, one question you may ask is, which should I use?
In most cases, using a Sling Models Interface fill require less code to accomplish the same task. The most common case where you would want to use a Class is if you need to do any filtering or manipulation of the values being returned. Using a Class for your model, you can then inject the values into the field and generate the needed return value
The following implementation to ascertain the above statement , where we can display the value of message variable as per our need.
A number of additional annotations can be used to modify the behavior of the Class , please refer the Sling Model documentation.
Please leave any comments or feed back ,if you have any question or run into any issues
Comments
Post a Comment