Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Juan José Fuentes
Struts 2 + Post-Redirect-Get
November 13, 2012 9:04 AM
Answer

Juan José Fuentes

Rank: Junior Member

Posts: 41

Join Date: July 16, 2010

Recent Posts

I want to implement the PRG pattern with a Struts 2 portlet (using Jsr168Dispatcher so Portlet 1.0) in Liferay 5.2.3, having unsuccessful results.

I've been playing with the <action-url-redirect> tag in liferay-portlet.xml. If I set it to true, the navigation in the portlet seems to be completely broken.

I've check this post but the link to the supposed solution is broken.

The redirectAction doesn't seem to perform an http redirect but what it seems to be an internal redirect, so if the user refreshes the page after submiting a form with a HTTP POST method.

Has someone managed to implement this?

Thank you!
Roberto Díaz
RE: Struts 2 + Post-Redirect-Get
November 14, 2012 7:19 AM
Answer

Roberto Díaz

LIFERAY STAFF

Rank: New Member

Posts: 24

Join Date: October 30, 2012

Recent Posts

Strus2 taglib has a tag called <s:token> which is used for that porpouses.

This token interacts with a interceptor that you have to include to the default-stack of the action.

I recomend you to read the Struts2 documentation for more info
Juan José Fuentes
RE: Struts 2 + Post-Redirect-Get
November 14, 2012 9:19 AM
Answer

Juan José Fuentes

Rank: Junior Member

Posts: 41

Join Date: July 16, 2010

Recent Posts

Roberto, as I checked in struts 2 docs, yes, the <s:token> is a way to overcome the double-posting of a form by placing a hidden token in the form, but what I really want is to perform the redirect after the post submission to avoid the "Do you want to resend the data"-like message when the user refreshes the page after form submission.

In a non-portal, non liferay struts 2 application, the redirectAction result type for the struts 2 action works perfectly well, in the way that the browser after the POST operation, automatically executes a GET operation to the action that is configured in the result of the first action.

I mean this:

 1
 2<action name="action1" class="com.test.Action1">
 3    <result type="redirectAction">
 4        <param name="actionName">action2</param>
 5        <param name="namespace">/minamespace</param>
 6    </result>
 7</action>
 8
 9<action name="action2" class="com.test.Action2">
10    <result>/redirected.jsp</result>
11</action>


After executing action1, the browser receives a response with a HTTP status of temporarily moved, and a location to perform a redirection with a GET http method to action2. In my specific case of using Struts2 in Liferay, there is no redirection. The portlet directly shows the result of action2, but the browser has only performed the first HTTP POST operation, not a GET one after it. So, if the user refreshes the page, the browser tries to execute the POST rather than the desired GET,
Roberto Díaz
RE: Struts 2 + Post-Redirect-Get
November 14, 2012 11:05 AM
Answer

Roberto Díaz

LIFERAY STAFF

Rank: New Member

Posts: 24

Join Date: October 30, 2012

Recent Posts

Have you tried http://struts.apache.org/2.x/docs/redirect-result.html?
Juan José Fuentes
RE: Struts 2 + Post-Redirect-Get
November 15, 2012 8:07 AM
Answer

Juan José Fuentes

Rank: Junior Member

Posts: 41

Join Date: July 16, 2010

Recent Posts

Doesn't work either. A PortletException is thrown from PortletResult.class, in the struts-portlet-plugin jar, saying that a dispatcher could not be found. I've checked the code from the struts portlet plugin and is launched from these lines of code:

 1
 2 PortletRequestDispatcher dispatcher;
 3        if (useDispatcherServlet) {
 4            req.setAttribute(PortletConstants.DISPATCH_TO, finalLocation);
 5            dispatcher = ctx.getNamedDispatcher(dispatcherServletName);
 6            if(dispatcher == null) {
 7                throw new PortletException("Could not locate dispatcher servlet \"" + dispatcherServletName + "\". Please configure it in your web.xml file");
 8            }
 9        } else {
10            [b]dispatcher = ctx.getRequestDispatcher(finalLocation);[/b]
11           [b] if (dispatcher == null) {
12                throw new PortletException("Could not locate dispatcher for '" + finalLocation + "'");
13            }[/b]
14        }


Doing test, if I try to put 'http://www.google.com/' as the location, a dispatcher could not be found. What I really want to do is a url redirect to the same portlet, but I'm not able to generate a value that is accepted in a portlet environment... emoticon
Roberto Díaz
RE: Struts 2 + Post-Redirect-Get
November 15, 2012 9:43 AM
Answer

Roberto Díaz

LIFERAY STAFF

Rank: New Member

Posts: 24

Join Date: October 30, 2012

Recent Posts

I think that this issue is related with the Struts2 Portlet Plugin.

The last ideas I have:

Try to update the libraries

Try to chain the action instead of redirect it.
Juan José Fuentes
RE: Struts 2 + Post-Redirect-Get
November 27, 2012 12:15 PM
Answer

Juan José Fuentes

Rank: Junior Member

Posts: 41

Join Date: July 16, 2010

Recent Posts

What I finally did was to create a new Struts 2 result type that handled the redirection according to my needs. Really don't know why the previous configurations were not working...

Thanks a lot Roberto emoticon