Link to home
Start Free TrialLog in
Avatar of abel
abelFlag for Netherlands

asked on

How to get the current JSP PageContext from an Action with Struts

Hi Experts!

I've been struggling with this seemingly easy question for some time now. Maybe my question is wrong, because what I am actually after is how to do a RequestUtils.computerUrl(). In the (very) old Struts 1.0.2, which I am unfortunately bound to during the current change in a legacy application, it seems about the only trustworthy way to calculate the absolute URL for doing a redirect action, based on an ActionForward.

I hope I am not too vague. I need to construct a URL, either absolute or relative to the current page context, based on an ActionForward, with a Map of parameters. I was construction the URL by hand, but found out that it is erroneous, because the current path is not known and the context path (to which the ActionForward is relative) is not known either.

The computeUrl() method requires as first parameter (whoever knows why) a  PageContext. I can store the current pageContext from the JSP file, but then I violate the MVC pattern (apart from being annoying).

I have found the JspFactory.getPageContext(), but that is used for initializing the page context, which is obviously not what I need here. Something like a static method of PageContext.getCurrentContext() I haven't yet found. I saw some references on the internet and some unanswered questions, none provided an answer. Though it shouldn't be too hard in a context-central environment to retrieve the context, shouldn't it?

For what it's worth, I use IBM WebSphere 5.1.1, which runs on JDK 1.4, with Struts 1.0.2 (yes, I know, it is old).

I am not looking for an answer for how to concatenate the different pieces of the URL, I can do that much myself, I'm after a stable distributable method that will construct the URL correctly using an ActionForward as a reference.

Thanks in advance,
-- Abel --

PS: I'm rather acquainted with newer JSP, JSTL, J2EE, Struts 2 etc, I've never worked with the ancient Struts 1.0.2 before, maybe it was just not provided for yet?
Avatar of boonleng
boonleng
Flag of Malaysia image

If not mistaken, the method RequestUtils.computerUrl() is designed for struts tag, and not for action class,
coz this method has moved to TagUtils in later version.

What I suggest is that you base on the RequestUtils.computerUrl() source code to write a similar method like
CutomRequestUtils.computeURL(ServletContext context, HttpServletRequest request, ActionForward forward, Map params)

coz the PageContext is mainly use for retrieving the ServletContext and HttpRequest.
Avatar of abel

ASKER

So, basically what you are suggesting is doing it by hand (or, based on the open source of the current computeUrl() method).

In a way, it should be enough to have an ActionForward and the ServletContext or HttpServletRequest  (to find the root of the servlet) to find the absolute url for a forward. I am surprised there is no programmatic way to do so.

Do you know of a helper method that does this for me, including merging the params Map?
Avatar of abel

ASKER

What I have is the situation that in a legacy application a couple of JSP files exist that must be forwarded (redirected) when a certain requirement is met. The requirement (a state) is stored in the session.

Some JSP have a Struts Action. Hence I wanted to use the existing MVC pattern and not break it.

However, I the non-form JSP files need to be redirected sometimes too. I'd like to have it configurable and I hoped to use the struts-config for it. Is it possible to add an action that does nothing, except redirect or show the JSP page? Or am I making things to hard and should I just break the MVC pattern?

any help or thoughts is appreciated,

Cheers,
-- Abel --
ASKER CERTIFIED SOLUTION
Avatar of boonleng
boonleng
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abel

ASKER

To answer your last question: sometimes the moment of redirection is from a JSP and sometimes it is from an Action. Sometimes the location is at the root of the service (context) and sometimes it is at some arbitrary depth (several folders deep). The hostname can be different, too.

The largest problem (in lieu of the above) was that sometimes the redirect was internal (ActionForward) and sometimes it was external (response.sendRedirect). To add to the confusion, the whole application is build on a staggering 13 (!) frames and framesets that have inter-relations that will make your head spin.

That's the legacy. I hoped to find one-size-fits-all solution, hence the trying of either getting the servlet context and/or the JSP it is all coming from to build the redirection url. This was leading to only more spaghetti in the end: trying to keep within the bounds of Struts in an application that already violates it on many places proved to be largely impossible.

Making structure from a mess is hard (try to build a house from a ruin is harder than building a house from scratch) but I am not allowed to rebuild (budget and other reasons). Meaning that in the end I end up placing a new window every here and there.... :S

My best-fit solution so far (I have tried three extensible approaches) is to use a dedicated page that mimics some of the frame-behavior of existing pages. As it starts to look now, it may be the correct approach.

Thanks for the help so far,

Cheers,
-- Abel --
Avatar of abel

ASKER

Ok, by now I have found a couple of other workarounds to this. Basically, I use the HttpServletRequest object to store and retrieve the values and I changed the code such that I do not need the JspContext anymore (bad practice, it seemed now that I understand more of this codebase).

Because you've been the only one around that has helped me out, I'll grand you the points.
Avatar of abel

ASKER

though the solution was not used in the end, thanks for the help!