首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在GWT中将CSS文件链接到HTML

如何在GWT中将CSS文件链接到HTML
EN

Stack Overflow用户
提问于 2019-06-12 15:36:29
回答 1查看 122关注 0票数 0

在客户端的GWT应用程序中,我生成了一个包含HTML内容的字符串,并将其传递给一个函数,该函数在新选项卡中打开它。我已经编写了一个CSS文件来设置HTML内容的样式,并给出了一个指向它的链接。但是我的HTML文件没有样式化。

代码语言:javascript
复制
public void writeHtml(){
    StringBuffer html = new StringBuffer();
    html.append("<!DOCTYPE html>");
    html.append("<html lang=\"en\">");
    html.append("<head>");
    html.append("<title>Hello World</title>");
    html.append("<meta charset=\"utf-8\">\n" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");

    html.append("<link href=\"StyleSheet.css\" rel=\"stylesheet\" type=\"text/css\">");

    html.append("</head>");
    html.append("<body>\n" +
                "\n" +
                "<h1>This is a heading</h1>\n" +
                "<p>This is a paragraph.</p>\n" +
                "\n" +
                "</body>\n" +
                "</html>");
    openPrintWindow(html.toString());
}

public native void openPrintWindow(String contents) /*-{
    var printWindow = window.open("", "PrintWin", false);
    printWindow.document.open("text/html","replace");
    if (printWindow && printWindow.top) {
        printWindow.document.write(contents);

    } else {
        alert("The print feature works by opening a popup window, but our popup window was blocked by your browser.  If you can disable the blocker temporarily, you'll be able to print here.  Sorry!");
    }
}-*/;

CSS文件- StyleSheet.css

代码语言:javascript
复制
h1 {
  color: blue;
  font-family: verdana;
  font-size: 300%;
}
p  {
  color: red;
  font-family: courier;
  font-size: 160%;
}

那么,问题出在哪里,我错在哪里呢?

EN

回答 1

Stack Overflow用户

发布于 2019-06-13 07:30:57

在你写的一条评论中:

这两个文件都在同一个文件夹中。所以,我想这条路是正确的。

那是胡说。您需要区分源文件夹和公用文件夹。如果您可以链接源文件夹中的文件并使其可供下载,这将是非常不安全的。

你应该读一读关于Standard Directory and Package Layout的文章。在那里你会发现:

源文件夹-包含生产Java源

war文件夹-您的web应用程序;包含静态资源和编译输出

这意味着您需要将StyleSheet.css文件放在war文件夹中。

在将StyleSheet.css文件复制到war文件夹(并修复了您发布的代码中的一些拼写错误-请参阅我的编辑)之后,我得到了以下内容作为它工作的证明:

作为进一步的阅读,我建议使用Modules: Units of configuration文档中的Public Path部分。

在说完上述所有内容之后:如果您只需在war文件夹中创建一个静态html文件,并打开一个新窗口并将指向此文件的链接作为参数,则会容易得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56556903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档