Expand my Community achievements bar.

SlingHttpServletRequest adapto custom sling model

Avatar

Level 3

Hi There ,

 I'm using a custom sling model which i have annotated to be adapto from resource and singHttpServletRequest .

I have designed my model with the fields same as in my form ,

Example : My form has fields "Name", "Age," "Address"

my model also has these 3 fields all injected using "

@Inject @Named (field name from form)

"

I was assuming doing this would automatically map my request parameters to sling model if i perform request.adapto(customodel.class) .However this doesn't seem to work .

Is my understanding right in doing this ? Did i miss anything here ?

 

regards

8 Replies

Avatar

Administrator

Hi

[Not a Solution but a good read]

Link:- http://www.wemblog.com/2014/11/how-to-use-sling-models-in-cq56.html

//How to use Sling Models in CQ5.6

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Community Advisor

Hi Kris,

 

You could use the RequestAttribute[1] annotation that has been created for your use case.

[1] http://grepcode.com/file/repo1.maven.org/maven2/org.apache.sling/org.apache.sling.models.api/1.2.0/o...

 

Regars,

Peter

Avatar

Level 3

Hi ,

Thanks for your pointers . 

Regarding RequestAttribute , am not sure if requestParameter will map to requestAttribute  . Will give it a try though  . will keep you posted.

Avatar

Level 3

Just an update ,

So I gave a try injecting request params to my model vis RequestAttribute injection , but doesn't seem to work ..

Inject @Named(field name from form) @Source("request-attributes") .

 

I guess in Springs there is a class RequestDataBinding which does exactly this . I wonder if something like this also exists for sling ?

Avatar

Level 10

Once you have adapted your resource to slinghttprequest in sling model

All you need to do is using below code to get properties saved in node

ValueMap node = request.getResource().getValueMap();
String path = node.get("text", String.class);

Here I am retrieving 'text' property

Do see this article to get more idea:

http://adobeaemclub.com/sightly-pagination-component-using-sling-model-and-javascript-use-api-in-aem...

Avatar

Level 3

Thanks for the reply. 

But this approach is leading me to the impression that I need to manually map request Params to custom mode by get and set. 

I had already used annotations to map req Params to sling model shouldn't be performing it manually again. Spring requestdatabinder does exactly this. Waiting for the same in sling