aspose file tools*
The moose likes Struts and the fly likes Issue with Struts 2 Dispatcher Result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Issue with Struts 2 Dispatcher Result" Watch "Issue with Struts 2 Dispatcher Result" New topic
Author

Issue with Struts 2 Dispatcher Result

Vivek Chaitanya
Ranch Hand

Joined: Oct 19, 2007
Posts: 35
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

Joined: Oct 19, 2007
Posts: 35
Formatted the code...

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vivek Chaitanya
Ranch Hand

Joined: Oct 19, 2007
Posts: 35
Hi Ankit,

Thanks for your reply.

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

Regards
Chaitanya
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 19, 2007
Posts: 35
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

Can you show us the code for that action which would make it easier to find out the problem...
Vivek Chaitanya
Ranch Hand

Joined: Oct 19, 2007
Posts: 35
In execute() method:




In JSP


Vivek Chaitanya
Ranch Hand

Joined: Oct 19, 2007
Posts: 35
And I am using dispatcher result type as below:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 19, 2007
Posts: 35
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 16, 2009
Posts: 2
>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

Joined: Sep 29, 2008
Posts: 12617

How on earth is request.setAttribute("name", "the name") easier than name = "the name"?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Issue with Struts 2 Dispatcher Result