在RAP的SWT浏览器小部件中使用JavaScript显示本地HTML文件 [英] Show local HTML file with JavaScript in SWT Browser widget in RAP

查看:90
本文介绍了在RAP的SWT浏览器小部件中使用JavaScript显示本地HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的RAP项目,我需要显示一些图表.因为我没有为此目的找到任何小部件,所以我的计划是使用浏览器小部件,因此我可以使用Highcharts或Chartsjs之类的JavaScript插件.但是我无法正常工作.如果我在browser.setUrl中设置了HTML文件,则浏览器小部件将不显示任何内容,甚至不显示简单的HTML. Chrome中的JavaScript控制台说

For my RAP-project I need to show some charts. Because I haven't found any widget for this purpose, my plan was to use the browser widget, so I can use JavaScript-Plugins like Highcharts or Chartsjs. But I can't get it working. If I set an HTML-File in browser.setUrl, the browser widget don't show anything, not even simple HTML. The JavaScript-Console in Chrome says

不允许加载本地资源

Not allowed to load local resource

如果我使用setText方法输入HTML代码,它将显示HTML,但是JavaScript无法正常工作,它不会像jQuery库一样加载外部JS文件.

If I enter the HTML-Code with the setText method it shows the HTML, but JavaScript is not working, it don't load external JS-File like the jQuery-library.

这样不能做到吗?还是我的失败在哪里? (抱歉,我的英语不好,我不是母语人士.)

Can't this be done this way? Or where is my failure? (Sorry for my bad englisch, I'm not native speaker.)

这是我尝试过的Java代码:

Here's the Java-Code I tried:

browser = new Browser(composite, SWT.NONE);
browser.setTouchEnabled(true);
browser.setBounds(10, 10, 358, 200);
browser.setUrl("D:\\STATS\\statistiken.html");

或者这个:

File file = new File("D:\\STATS\\statistiken.html");
browser = new Browser(composite, SWT.NONE);
browser.setTouchEnabled(true);
browser.setBounds(10, 10, 358, 200);
browser.setUrl(file.toURI().toString());

我也尝试了其他一些方法,但是没有用.

I tried also some other things, there were not working to.

在setText方法中使用HTML(我在同一文件夹中尝试了外部库和本地库):

With HTML in setText-method (I tried external libraries and local libraries in same folder):

browser = new Browser(composite, SWT.NONE);
browser.setBounds(10, 10, 358, 200);
browser.setText(
    "<html>" +
    "<head>" +
        "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js\"></script>" +
        "<script src=\"http://code.highcharts.com/highcharts.js\"></script>" +
        "<script src=\"http://code.highcharts.com/modules/exporting.js\"></script>" +
    "</head>" +
    "<body>" +
        "<p>Test</p>" +
        "<div id=\"container\" style=\"min-width: 400px; height: 400px; margin: 0 auto\"></div>" +
    "</body>" +
    "</htm>");

希望有人可以帮助我解决这个问题.

Hope someone can help me with this problem.

推荐答案

在您的情况下,本地链接将无法解析,并且外部链接也不会加载(跨域问题).

Local links will not be resolved and external links will not be loaded(Cross Domain problem) in your case.

我可以建议您2个解决方案.

I could suggest you 2 Solutions.

当您只有很少的资源(html,javascript,css)可在Browser上呈现而没有Hyperlinks(当按clike时将加载不同的页面)时,这很有用.

This is useful when you have very few resources(html, javascript, css) to render on Browser and no Hyperlinks(which when cliked will load a different page).

您可以使用速度.阅读,以开始使用Velocity.

You can use Velocity. Read this to start using Velocity.

您可以将所有静态内容包含在Velocity Template中,并在运行时将动态内容注入其中.

You can have all the static content in Velocity Template and inject Dynamic content into it at Runtime.

这是我的一个项目的摘录.

Here is the excerpt from one of my Projects.

init.vm

 <html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
.transcript {
    background-color: #d2d2d2;
}

.messageBlock {
    margin-left: 4px;
    margin-bottom: -15px;
}

.message {
    margin-left: 115px;
    word-wrap: break-word;
    white-space: -moz-pre-wrap;
    _white-space: pre;
    white-space: pre-wrap;
}
</style>

</head>
<script type="text/javascript">
function resizeChatWindow() { var divT = document.getElementById("divTranscript"); divT.style.height = (document.body.clientHeight - getTopAreaHeight()) + "px"; divT.style.width = (document.body.clientWidth) + "px"; divT.style.overflow = "auto"; divT.style.position = "absolute"; divT.style.left = "0px"; divT.style.top = getTopAreaHeight() + "px";}
 function getTopAreaHeight() { var chatAlert = document.getElementById("chatAlert"); if (chatAlert) { return chatAlert.clientHeight; } return document.getElementById("divBody").clientHeight;}
 isChat=false; window.onresize=resizeChatWindow;
</script>
<script type="text/javascript">
$scriptText
</script>

<script type="text/javascript">
function addChat(chatText){
    $("#divTranscript").append(chatText);

    $("#divTranscript").animate({ scrollTop: $("#divTranscript")[0].scrollHeight }, "slow");

}

</script>
    <body onload="resizeChatWindow();">
        <div id="divBody"></div>
        <div id="divTranscript">$history</div>
    </body>
</html>

VelocityUtils

private void init() throws Exception {
    ve = new VelocityEngine();
        Properties velocityProperties = new Properties();
        velocityProperties.put("resource.loader", "class");
        velocityProperties.put("class.resource.loader.description", "Velocity Classpath Resource Loader");
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        ve.init(velocityProperties);
        //ve.init();
}


public String getInitHtml(String history) throws ResourceNotFoundException, ParseErrorException, Exception {
    /* now render the template into a StringWriter */
    StringWriter writer = null;
    /* next, get the Template */
        Template t = ve.getTemplate("templates/init.vm","UTF-8");
        /* create a context and add data */
        VelocityContext context = new VelocityContext();
        String script = IOUtils.toString(VelocityUtils.class.getResourceAsStream("script/jquery.min.js"), "UTF-8");
        context.put("scriptText", script); //You can even have all the script content in init.vm rather than injecting it at runtime.
        context.put("history", StringUtils.defaultIfBlank(history, StringPool.BLANK));
        writer = new StringWriter();
        t.merge(context, writer);
        /* show the World */

    String returnMe = writer.toString();
    return returnMe;
}

Browser.setText()

我解释了它这里.

这篇关于在RAP的SWT浏览器小部件中使用JavaScript显示本地HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆