views:

58

answers:

1

I want to add a URL parameter before forwarding to another action from an action method. Although HttpServletRequest has a getParameter() method, it has no setParameter() method. I know setAttribute() is there but I need the it to be part of the URL (like ?something=something&some2=some2). I know I can do it using filters but that's an overkill for this task.

Basically we have an externally managed filter which will change something on a page when that parameter is set. Let's say the color of the page will be passed as part of the URL parameter. When that parameter is present a servlet filter picks it up and changes the color of that page. I know it's a very odd way of doing but that's how they have it set up here.

I know how to make it work using java script based form submit by adding the URL parameter to the action url (ie. "/someAction.do?color=red"). But for some of the actions it actually does a action.forward("action_name") in the code. For those I was wondering what to do.

Does anyone know how to do that in struts 1.2?

+1  A: 

The short answer is that it isn't possible. Request parameters are supposed to be from the HTTP request. You can fake adding them using a combination of a ServletFilter and an HttpServletRequestWrapper but that is outside of Struts. Depending upon what you are trying to accomplish there may be a better solution. Want to describe that a bit more?

Update

With the additional detail you've added, I think you can try this to see if it meets your needs:

import org.apache.struts.action.ActionRedirect;
...
ActionForward forward = action.forward("action_name");
ActionRedirect redirect = new ActionRedirect(forward);
redirect.addParameter("color", "red");
return redirect;
laz
Yes, I know I can do it using filters but that's too much work for just setting a URL parameter.
CoolBeans
Why can't you use setAttribute on the request object?
laz
Because the filter that changes the color of the page needs it as a request parameter. I have thought about this quite a bit, I don't see how to solve this without adding the url parameters via java script.
CoolBeans
I just saw that you edited your question before and added more detail. I'll update my answer.
laz
I think org.apache.struts.action.ActionRedirect is not available in struts 1.2 unless I am doing something silly.
CoolBeans
Looks like it was added in 1.2.7. See http://struts.apache.org/1.2.9/api/org/apache/struts/action/ActionRedirect.html
laz
Yeah - but I like your solution. Thanks Iaz!
CoolBeans