Message Boards

How do i redirect URL using FreeMarker ?

thumbnail
Achmed Tyrannus Albab, modified 4 Years ago.

How do i redirect URL using FreeMarker ?

Regular Member Posts: 158 Join Date: 3/5/10 Recent Posts
Hi,

I'd like to know how to redirect a page using freemarker (preferably) in structure and templated web content.
For example:
<#if is_signed_in>
${load google.com} this line
<#else>
${load yahoo.com} and this line
</#if>
Thanks in advance.
thumbnail
Christoph Rabel, modified 4 Years ago.

RE: How do i redirect URL using FreeMarker ?

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Is javascript sufficient?
<#if is_signed_in>
<script>
[url=http://window.location.href='https://....']window.location.href='https://....';
</script>
...
Otherwise it will be very hard.  I don't remember if there is a ${response} variable, but maybe you could do something with it. If it isn't there you should be able to get it from themedisplay.getResponse(). Probably it isn't the real HTTPServletResponse and you need to call some further methods to get it. With that you might be able to send a redirect.
But I would try to avoid that. A content should not override the page and send a redirect from the backend. The system is actually designed to prevent that and even if it works with you Liferay version, it might break and stop working with the next patch.
If possible go with the javascript.
thumbnail
Achmed Tyrannus Albab, modified 4 Years ago.

RE: How do i redirect URL using FreeMarker ?

Regular Member Posts: 158 Join Date: 3/5/10 Recent Posts
Thats the problem, i already have the javascript solution.
I am looking for the freemarker or backend way is because i need it to be done before the page loads.
If i use JS the load will be loaded first and break my design - long story. Thank you for the effort none the less.
thumbnail
Jan van der Kaaden, modified 4 Years ago.

RE: How do i redirect URL using FreeMarker ?

Junior Member Posts: 28 Join Date: 3/20/11 Recent Posts
Hi Achmed,It is probably not an elegant solution but you could consider to redirect using an MVC Portlet.But you must be careful and make sure you only redirect under predefined conditions so you don’t make your page inaccessible. 
Something like this will do the trick but once again you need a very good reason to want something like this...
public class RedirectPortlet extends MVCPortlet {  @Override  public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {     HttpServletResponse httpRequest = PortalUtil.getHttpServletResponse(renderResponse);     httpRequest.sendRedirect("/home");     super.doView(renderRequest, renderResponse);  }}