最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Open local HTML in new tab from RAP application - Stack Overflow

programmeradmin5浏览0评论

The users of the RAP app I am working on want a button to open some documentation in a new tab. This documentation lives in a local directory inside the Docker container running the app, let's say /opt/doc/index.html.

I created said button, and gave it the following action:

public class OpenDocumentationAction extends Action
{
    public static String DOCUMENTATION_URI = "/opt/doc/index.html"; 
    
    public OpenDocumentationAction()
    {
        // Nothing to do here
    }
    
    /** {@inheritDoc} */
    @Override
    public void run()
    {
        File htmlFile = new File(DOCUMENTATION_URI);
        String path = htmlFile.getAbsolutePath();
        UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
        launcher.openURL(";);
    }    
}

You can see that I gave the Google URL to the openURL method. This is because this one works, it opens in a new tab like I want. The moment I pass it my DOCUMENTATION_URI String, it does nothing. No error message, no exception, just nothing.

My current theory is that the browsers (I tested with Firefox and Chrome) have a security feature to block webapps from opening local files like this. Regardless of if I am correct or not, I need a way to open that local HTML file.

The users of the RAP app I am working on want a button to open some documentation in a new tab. This documentation lives in a local directory inside the Docker container running the app, let's say /opt/doc/index.html.

I created said button, and gave it the following action:

public class OpenDocumentationAction extends Action
{
    public static String DOCUMENTATION_URI = "/opt/doc/index.html"; 
    
    public OpenDocumentationAction()
    {
        // Nothing to do here
    }
    
    /** {@inheritDoc} */
    @Override
    public void run()
    {
        File htmlFile = new File(DOCUMENTATION_URI);
        String path = htmlFile.getAbsolutePath();
        UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
        launcher.openURL("https://www.google");
    }    
}

You can see that I gave the Google URL to the openURL method. This is because this one works, it opens in a new tab like I want. The moment I pass it my DOCUMENTATION_URI String, it does nothing. No error message, no exception, just nothing.

My current theory is that the browsers (I tested with Firefox and Chrome) have a security feature to block webapps from opening local files like this. Regardless of if I am correct or not, I need a way to open that local HTML file.

Share Improve this question asked Feb 17 at 16:33 GaëtanGaëtan 8631 gold badge11 silver badges33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Browsers block web apps from opening local files directly. Instead, serve the documentation via the RAP resource manager and open it with UrlLauncher.

Solution:
Register the resource:

RWT.getApplicationContext().getResourceManager().register(
    "doc/index.html", 
    new FileInputStream(new File("/opt/doc/index.html"))
);

Launch the URL:

UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
launcher.openURL(RWT.getRequest().getContextPath() + "/rwt-resources/doc/index.html");

This makes the file accessible via a URL within your app and opens it in a new tab.

发布评论

评论列表(0)

  1. 暂无评论