Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem getting attributes from request in Struts2

 
Sammy Bill
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That'd do it ;)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic