Skip to main content
Dterner
Level 2
Oct 16, 2015 2:28 AM UTC
Solved

way of setting and retrieving a Page object via request attribute

Hi,

It looks like setting and retrieving a request attribute is a good way to pass data between several .jsp files in a component. I am trying to pass a Page object and keep getting a "type mismatch". I'm sure its something simple, but I couldn't find that exact use case googling around a bit.

So in jsp 1 the setting code is:

request.setAttribute("page", page);

in jsp 2 the getting code is:

Page myPage = request.getAttribute("page");

Thanks!

- Dmitry

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
brucelefAccepted solution
Adobe Employee
Oct 16, 2015 2:28 AM UTC

Have you tried casting the Page object from the request attribute?

See the foundation list component for an example [0][1].

[0] /libs/foundation/components/list/init.jsp and

[1] /libs/foundation/components/list/list.jsp (line 36, cast to List in this case)

10 replies

Dterner
DternerAuthor
Level 2
Oct 16, 2015 2:28 AM UTC

Another update. I marked Bruce's answer as correct. 

new code: 

 

Page page = (Page)request.getAttribute("page");

Thanks Bruce! 

Translate
Dterner
DternerAuthor
Level 2
Oct 16, 2015 2:28 AM UTC

I do have an update, I changed the code around a bit and it works, but I do feel like there way a way to do it in one line like I wanted originally. For edification purposes I am now doing:

Setting like this:

Page page = <Page>; request.setAttribute("page", page.getPath());

Getting like so:

String page = request.getAttribute("page").toString(); Page myPage = pageManager.getPage(page);

That works. If anyone has better ways of doing this, I'd love to learn.

Thanks

Translate
Dterner
DternerAuthor
Level 2
Oct 16, 2015 2:28 AM UTC

It does not, it's actually the other way around the setting jsp calls the getting jsp. The other way doesn't really make sense in most use cases. It looks like the object is being passed though, if I get it via generic object:

Object myPage = request.getAttribute("page");

It does retrieve an object, I just can't convert it to an object of type Page.

Translate
Adobe Employee
Oct 16, 2015 2:28 AM UTC

Does the script in which you are calling getAttribute include the script that is setting it (via <cq:include script=""/>)?

Translate
Dterner
DternerAuthor
Level 2
Oct 16, 2015 2:28 AM UTC

Thanks Bruce, I actually am. The above sample is just for clarity.

Translate
brucelefAdobe EmployeeAccepted solution
Adobe Employee
Oct 16, 2015 2:28 AM UTC

Have you tried casting the Page object from the request attribute?

See the foundation list component for an example [0][1].

[0] /libs/foundation/components/list/init.jsp and

[1] /libs/foundation/components/list/list.jsp (line 36, cast to List in this case)

Translate
Dterner
DternerAuthor
Level 2
Oct 16, 2015 2:28 AM UTC

Thanks Scott, That was my thought as well. When I do this: 

Page myPage = request.getAttribute("page").adaptTo(Page.class);

I get an error saying:

"The method adaptTo(Class<Page>) is undefined for the type Object"
Translate
Dterner
DternerAuthor
Level 2
Oct 16, 2015 2:28 AM UTC

Scott, I think I'm missing something fundamental here. What I'm trying to do is pass an object from one jsp to another.

I am doing this via setting and getting a request object. This is not information stored in the crx but rather dynamic content generated at runtime.

I guess what i'm not getting from your response is, are you saying that is the wrong approach to this problem, or am I not understanding how to get the object from the request and convert to a resource first?

I really appreciate the help.

Translate
smacdonald2008
Level 10
Oct 16, 2015 2:28 AM UTC

You can get page props and display them in an AEM component. 

You can use Sling APIs and the adaptTo method for this use case. 

Page page = res.adaptTo(Page.class);

See this community article to learn how to use the Sling API:

http://scottsdigitalcommunity.blogspot.ca/2013/09/using-sling-to-retrieve-content-from.html

Translate
Adobe Employee
Oct 16, 2015 2:28 AM UTC

Try using a name other than page for the request attribute:

request.setAttribute("myPage", myPage);

Edit: other request attribute names to avoid: http://dev.day.com/docs/en/cq/current/developing/scripts.html#<cq:defineObjects>

Translate