This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes Problem getting attributes from request in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem getting attributes from request in Struts2" Watch "Problem getting attributes from request in Struts2" New topic
Author

Problem getting attributes from request in Struts2

Sammy Bill
Ranch Hand

Joined: Dec 29, 2008
Posts: 96
Hello,

In my action when i set an attribute in the request object i can not retrieve it from the jsp, but when i put it in the session, i m able to retrieve it.

how can i set the request attribute in my action to use it in the jsp.

here is my action and my jsp





in the jsp when i do

<c:forEach var="product" items="${session.catalog}" >

and when i do <%=request.getAttribute("catalog")%> it prints null.

it works fine.

how can i retrieve the data from the request scope?

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

and when i do <%=request.getAttribute("catalog")%> it prints null.

it works fine.


Which is it?

That aside, you're doing two different things: you're accessing the session in the JSP via an action property (which is fine, but a little weird). You're accessing the request via JSTL's normal implicit object. Whether or not you *should* be storing this data in the request or session is debatable--personally it seems like it belongs in either the application scope or in no scope at all and should be exposed via an action property.

Note also that it's *much* cleaner to implement RequestAware to get the request's attribute maps, and has the benefit of not tying your action to the servlet spec.

Lastly, without knowing the result type it's hard to say--I would have thought a standard "dispatcher" result type would have worked.
Sammy Bill
Ranch Hand

Joined: Dec 29, 2008
Posts: 96
David Newton wrote:
That aside, you're doing two different things: you're accessing the session in the JSP via an action property (which is fine, but a little weird). You're accessing the request via JSTL's normal implicit object. Whether or not you *should* be storing this data in the request or session is debatable--personally it seems like it belongs in either the application scope or in no scope at all and should be exposed via an action property.

Note also that it's *much* cleaner to implement RequestAware to get the request's attribute maps, and has the benefit of not tying your action to the servlet spec.

Lastly, without knowing the result type it's hard to say--I would have thought a standard "dispatcher" result type would have worked.


i Tried to implement the RequestAware interface but i still can not access the request attributes from the jsp page while i can access the object if i store it in the session

here is the entire code:



and here is the jsp page.

<c:forEach var="product" items="${request.catalog}" >
Product: ${product.productID}

Description: ${product.description}

Quantity available: ${product.quantity}

Unit Price: ${product.unitPrice}
<s:form method="POST">
<s:textfield name="quantity" size="3"></s:textfield>
<s:submit value="add to cart"></s:submit>
</s:form>
</c:forEach>

the following page does not display anything.
but if i used <c:forEach var="product" items="${session.catalog}" >

the data is displayed correctly.

any idea why?

Thanks


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I wrote: Lastly, without knowing the result type it's hard to say--I would have thought a standard "dispatcher" result type would have worked.
Sammy Bill
Ranch Hand

Joined: Dec 29, 2008
Posts: 96
David Newton wrote:
I wrote: Lastly, without knowing the result type it's hard to say--I would have thought a standard "dispatcher" result type would have worked.


Sorry, I did not understand what do you mean by the result type.

Can you explain more?

Thanks for your time.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

When you define an action in your XML you define its results.

Every result has a type.

What's the type of the result you're using?
Sammy Bill
Ranch Hand

Joined: Dec 29, 2008
Posts: 96
David Newton wrote:When you define an action in your XML you define its results.

Every result has a type.

What's the type of the result you're using?


Ah ok, I see

I m was using redirect as a type, I used a regular dispatcher and it worked fine.

Thanks a lot.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That'd do it ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem getting attributes from request in Struts2
 
Similar Threads
servlet mapping
Accessing session scope attributes
Form Params not passed properly
Implementing HttpSession in the Spring MVC
Tag Files and attributes