Expand my Community achievements bar.

SOLVED

way of setting and retrieving a Page object via request attribute

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Employee

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)

View solution in original post

10 Replies

Avatar

Employee

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>

Avatar

Level 10

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

Avatar

Level 2

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.

Avatar

Level 2

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"

Avatar

Correct answer by
Employee

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)

Avatar

Level 2

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

Avatar

Employee

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

Avatar

Level 2

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.

Avatar

Level 2

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

Avatar

Level 2

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

new code: 

 

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

Thanks Bruce!