Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with Struts 2 Dispatcher Result

 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

We are using Struts 2 Dispatcher Result to forward our request to some jsp page.

In struts.xml


Now we are able to see success.jsp but the request attributes we set are becoming null in success.jsp

We used dispatcher because we need the same request to be forwarded so that the request attributes we set can be retrieved in success.jsp But it is not happening and we are getting nulls instead.

Is there anything which needs to be taken care?

Thank you Chaitanya
 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Formatted the code...

 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the action code as to how you are setting request attributes.

PS: The dispatcher result is the default result type if you extend the struts-default package...
 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,

Thanks for your reply.

We are setting request attributes using the standard request.setAttribute() method.

Regards
Chaitanya
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides that this isn't the canonical way to send data to a JSP in Struts 2...

Are you saying that you're calling request.setAttribute(...) in the method that then returns "success"? I don't see how that could fail to work.

By the way, "dispatcher" is the default result type, and the default param is "location"--your other result declarations are functionally equivalent to the "success" result you define by hand.
 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am calling request.setAttribute(...) in the method that returns success . i.e in execute method before returning success. I am not understanding why it is failing to set request attributes.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the code for that action which would make it easier to find out the problem...
 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In execute() method:




In JSP


 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I am using dispatcher result type as below:

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you obtaining the request?

If you're doing this correctly, calling the action you think you are, and showing the result you think you are, there's no way this can fail. While I don't do it often (Struts 2 has a better way to pass data to a JSP, as I've already stated), it's unlikely to be an S2 issue.

It'd be so much easier if you just included all the relevant code and configuration at once.
 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi david, Thank you for your reply.

Struts 2 has a better way to pass data to a JSP, as I've already stated


Can you please let me know what is the better way to pass data to JSP?

Regards
Chaitanya
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The canonical Struts 2 way is to just make an action property, which are accessible via JSTL or the S2 tags via OGNL.
 
j tan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>How are you obtaining the request?
I was wondering the same thing. Probably Vivek tried Struts 1.X where it is available as part of the formal parameters of execute method

There should be a way to access request object anyway, IMHO, I know Struts 2 propose setting/passing values via properties, but to be able to set request in attribute is so much easier.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How on earth is request.setAttribute("name", "the name") easier than name = "the name"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic