0

Currently I'm developing a Structure/Template combination in Liferay 6.2 and I discovered a problem.

In my Structure the user can type an url to an external site ("www.google.com" for example):

<dynamic-element dataType="string" indexType="keyword" localizable="true" name="website" readOnly="false" repeatable="false" required="false" showLabel="true" type="text" width="small">
        <meta-data locale="de_DE">
            <entry name="label">
                <![CDATA[Website (www.)]]>
            </entry>
            <entry name="predefinedValue">
                <![CDATA[]]>
            </entry>
            <entry name="tip">
                <![CDATA[]]>
            </entry>
        </meta-data>
    </dynamic-element>

In my Template i want a Link to that page, but the current code just appends the String Value of the Structure to the BaseURL of my site. Something like www.company-url.de/web/www.google.de

<a href="${website.getData()}">More information</a>

(I can't give the correct URLs because I'm not sure wether it's allowed)

Is there a way to tell Liferay to use the String as an independent url, instead of appending it?

Thanks a lot for your help.

Hookerino
  • 25
  • 4
  • You should check the definition of [URL](https://en.wikipedia.org/wiki/URL). `www.google.de` is the name of a host, but not an URL. An URL is always absolute - including the scheme. Everything else is expected to be relative to the current page. – Tobias Liefke Sep 22 '15 at 08:16
  • Thank you for teaching my such an fact. But i still think that people should still be able to understand what my problem is. And even if the user really types in an url, it's still just appened to the current page. – Hookerino Sep 22 '15 at 08:19
  • Please try it yourself: If the user enters an absolute URL (`http://www.google.de/`) it will _not_ be appended to the current page. – Tobias Liefke Sep 22 '15 at 08:30
  • And that's the problem. I cant be sure wether the user uses the scheme or not. Because of that the problem exists. And yes, i give you credit for the URL part, I haven't specified it correctly. – Hookerino Sep 22 '15 at 08:34
  • And whats the problem with `<#if !website.getData()?contains(":")>` as mentioned below? In that case only URLs are converted that are not absolute. – Tobias Liefke Sep 22 '15 at 08:57
  • For now this is the best solution. I'm just waiting a bit until I mark it as correct. – Hookerino Sep 22 '15 at 09:04

2 Answers2

0

Check if ${website.getData()} contains a ":", if not prepend it by yourself:

<#assign myURL = website.getData()>

<#if !website.getData()?matches(".*:.*")>
    <#assign myURL = "http://" + myURL>
</#if>

<a href="${myURL}">More information</a>

However, this is absolutely not enough to validate an URL...

Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
Marco Mercuri
  • 1,117
  • 9
  • 17
  • That's a solution i want to avoid, because im not sure what the User types in as url. He might write https:// for example too. I hoped there was a way to do this somehow automatically And isnt your if clause in the wrong order? I think you've got the "http://" twice in the url – Hookerino Sep 22 '15 at 07:57
  • 2
    You should use `contains(":")` then. The order is correct, as `!` is negating the condition. – Tobias Liefke Sep 22 '15 at 08:17
  • Oh didnt see the "!", sry ;) – Hookerino Sep 22 '15 at 08:27
-1

The problem is that you are using the method ".getData()", as you said it's just an string. Try using ".getText()" instead:

<a href="${website.getText()}">More information</a>

Hope it helps, and let me know if it works or not :)

Hexen
  • 53
  • 2
  • 14