Link to an External Website from data entered into Cuba forms

My forms are working as expected and my Java is constructing the URL as I need it, however I cannot seem to link to a site outside of the Cuba application.

Any hint?

1 Like

What about using the Link component?

1 Like

When I use the link component, the application is forcing me to another tab of the Cuba app.

I have the field setup as field name launch with as a string with Readonly and Transient enabled.

My Java for launch is:


public String getLaunch() {
    	if (port == 443) { String launch = "https://" + server + "/subapp/";
    	return launch;
    		} else { if (port == 80) { String launch = "http://" + server + "/subapp/";
    				return launch;} else { String launch = "http://" + server + ":" + port + "/subapp/"; return launch;}
    		}
    		
    }

Which is generating the correct information.

As displayed in the attachment link is set, however when I inspect the element it is displaying as a caption of that field instead of a generic link.


<div class="c-buttonfield v-widget v-readonly v-has-width" id="gwt-uid-423" aria-labelledby="gwt-uid-422" style="width: 310px;">
	<div tabindex="0" role="button" class="v-button v-widget link v-button-link">
		<span class="v-button-wrap">
			<span class="v-button-caption">http://server/subapp/</span>
		</span>
	</div>
</div>

Linkset

I have this somewhat working. I have the code working when it is deployed in debug on my local system.

Unfortunately when deployed on the server the URL launch is acting as if the button is not being clicked

public void onLinkButtonClick() {
    	VpsinformationCompany vpsinfo = new VpsinformationCompany();
    	String mylaunch = vpsinfo.newlaunch;
    	String os = System.getProperty("os.name").toLowerCase();
    	if (os.indexOf( "win" ) >= 0){
    		try {
    			Runtime rt = Runtime.getRuntime();
    			rt.exec( "rundll32 url.dll,FileProtocolHandler " + mylaunch);
    			} catch (IOException e) {
    				//System.out.println("THROW::: make sure we handle browser error");
    				e.printStackTrace();
    			}
    	if (os.indexOf( "mac" ) >= 0){
    		try {
    			Runtime rt = Runtime.getRuntime();
    			rt.exec( "open" + mylaunch);
    		} catch (IOException e) {
				//System.out.println("THROW::: make sure we handle browser error");
				e.printStackTrace();
			}
    	}
    	if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0){
    		try {
    			Runtime rt = Runtime.getRuntime();
    			String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror",
    			                                 "netscape","opera","links","lynx"};
		StringBuffer cmd = new StringBuffer();
    			for (int i=0; i<browsers.length; i++)
    			     cmd.append( (i==0  ? "" : " || " ) + browsers<i> +" \"" + mylaunch + "\" ");
    			rt.exec(new String[] { "sh", "-c", cmd.toString() });
    		} catch (IOException e) {
				//System.out.println("THROW::: make sure we handle browser error");
				e.printStackTrace();
			}
    	}

Any ideas

Additional information.

This is not related to Java versions. I just installed this locally on a new version of Java and Tomcat and the error exists there as well.

Any settings that I should look at that would make the interaction different from the built in Tomcat debug mode and deployed version of Tomcat?

Please look at this example of usage the Link component:


<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://caption"
        class="com.company.sample.web.screens.Screen"
        messagesPack="com.company.sample.web.screens">
    <layout>
        <hbox expand="link"
              spacing="true"
              width="100%">
            <textField id="textField"
                       textChangeTimeout="400"/>
            <button caption="Set link URL"
                    invoke="setUrl"/>
            <link id="link"
                  caption="http://cuba-platform.com"
                  target="_blank"
                  url="http://cuba-platform.com"/>
        </hbox>
    </layout>
</window>

public class Screen extends AbstractWindow {

    @Inject
    private TextField textField;
    @Inject
    private Link link;

    public void setUrl() {
        String value = textField.getValue();
        if (value == null)
            return;
        if (!value.startsWith("http://"))
            value = "http://" + value;
        link.setCaption(value);
        link.setUrl(value);
    }
}

The link URL must contain protocol part (http:// or https://).

This configuration is working when deployed!

Not exactly how I wanted the object to function. I was hoping for the button to launch the page to get the interaction down to 1 click instead of 2.

But this should work for now.

Thank you for your help.

Well, if you just want to open a web page, you actually don’t need the Link component, you can use the showWebPage() available in the controller. Or use them together, opening the web page and setting the URL into the link:


public void setUrl() {
    String value = textField.getValue();
    if (value == null)
        return;
    if (!value.startsWith("http://"))
        value = "http://" + value;
    link.setCaption(value);
    link.setUrl(value);

    showWebPage(value, ParamsMap.of("target", "_blank"));
}
1 Like

This is useful to me as well. Thanks!

This works perfect thank you!