Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Phil Gunbin
struts - forward from one action to another
May 12, 2008 5:31 AM
Answer

Phil Gunbin

Rank: New Member

Posts: 12

Join Date: March 3, 2008

Recent Posts

Hi!

By default, struts supports forwards (which are used in almost all cases) and redirects (when you need to simulate a fresh request from client). Struts in Liferay behaves in a different way because of some logic in PortalRequestProcessor.

PortalRequestProcessor overloads doForward() method which gets called when you return an ActionForward from your Action. Then, StrutsUtil.forward() is called in turn and it adds "/html" in front of forward path, thus meaning that only forwards to static resources (jsp, html etc) can be used. If you create dynamic ActionForward with path like themeDisplay.getPathMain() + "/c/portal/myaction", you'll get "resource not found" exception.

So the question is, is there a way to do a forward (for the request parameters to stay unchanged) from one struts Action to another? (both non-portlet).

Please correct me if I got something wrong.

Thanks.
Ray Augé
RE: struts - forward from one action to another
May 12, 2008 9:03 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1195

Join Date: February 7, 2005

Recent Posts

Hey Phil,

I've successfully done the following test:

- uncommented (from struts-config.xml)
1        <action path="/portal/test" type="com.liferay.portal.action.TestAction">
2            <forward name="portal.test" path="portal.test" />
3        </action>

-editted com.liferay.portal.action.TestAction.java adding these lines just before the return:
 1        boolean logout = ParamUtil.getBoolean(req, "logout");
 2
 3        if (logout) {
 4            ActionForward af = new ActionForward();
 5            af.setPath("/c/portal/logout");
 6            af.setRedirect(true);
 7
 8            return af;
 9        }

- then ant deploy followed by a portal startup.
- login
- hit /c/portal/test
- you should see some test output (default behavior)
- hit /c/portal/test?logout=true
- At this point you should have been logged out, meaning the redirect worked.

Can you try this just to see if we get the same results?
Phil Gunbin
RE: struts - forward from one action to another
May 12, 2008 9:15 AM
Answer

Phil Gunbin

Rank: New Member

Posts: 12

Join Date: March 3, 2008

Recent Posts

Hi Ray, thanks for answering

Redirect works OK surely. But I tried to do a forward.

*Action.java:

1ActionForward af = new ActionForward("/c/portal/my_action");
2
3/* possibly add here parameters via af.setPath() */
4
5af.setRedirect(false); /* default */
6
7return af;


And I got the behaviour I described just in case of forward.

Any ideas?
Ray Augé
RE: struts - forward from one action to another
May 12, 2008 10:00 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1195

Join Date: February 7, 2005

Recent Posts

Reproduced!

hmmm... I'm not sure what the best way to get around this... I'm not a struts expert by any means.

Wish I could be of more help. Perhaps if you explain your scenario a little it might shed some light on some possible solutions.
Phil Gunbin
RE: struts - forward from one action to another
May 12, 2008 10:54 AM
Answer

Phil Gunbin

Rank: New Member

Posts: 12

Join Date: March 3, 2008

Recent Posts

For static resources like JSP it works OK (StrutsUtil.forward() does it), but such implementation of PortalRequestProcessor misses a portion of default struts functionality.. I understand the behaviour and I've described it in the first post, but it is interesting for me whether it was done intentionally or not.

Also, the confusing thing in Liferay is the name of WebKeys.FORWARD_URL. It is named FORWARD but actually is used as REDIRECT (see forward_jsp.jsp) and its typical usage is

1req.setAttribute(WebKeys.FORWARD_URL, themeDisplay.getPathMain() + "/portal/next_action");
2return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);


and forward_jsp.jsp does
1response.sendRedirect(forwardURL);


Anyway thanks emoticon