简体   繁体   English

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

[英]How to link the CSS file to HTML in GWT

In my GWT application on client side I am generating a string that contains HTML content and passing it to a function which opens it in new tab.在客户端的 GWT 应用程序中,我生成一个包含 HTML 内容的字符串,并将其传递给在新选项卡中打开它的函数。 I have written a CSS file to style the HTML content and given a link to it.我已经编写了一个 CSS 文件来设置 HTML 内容的样式并给出了一个链接。 But my HTML file is not getting styled.但是我的 HTML 文件没有得到样式。

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 File - StyleSheet.css CSS 文件 - StyleSheet.css

h1 {
  color: blue;
  font-family: verdana;
  font-size: 300%;
}
p  {
  color: red;
  font-family: courier;
  font-size: 160%;
}

So, what is problem and where am I going wrong?那么,什么是问题,我哪里出错了?

You wrote html..append("");你写了 html..append(""); instead of html.append();而不是 html.append();

Does it answer your problem ?它是否回答了您的问题?

In one of your comments you wrote:在您的评论之一中,您写道:

Both the files are present in the same folder.这两个文件都存在于同一个文件夹中。 So, I guess the path is correct.所以,我想路径是正确的。

That's not true.这不是真的。 You need to distinguish your source folder from public folder.你需要从公用文件夹区分文件夹。 It would be very unsafe if you could link files in source folder and make them available for download.如果您可以链接文件夹中的文件并使其可供下载,那将是非常不安全的。

You should read about Standard Directory and Package Layout .您应该阅读有关标准目录和包布局的信息 There you'll find:在那里你会发现:

src folder - contains production Java source src 文件夹- 包含生产 Java 源代码

war folder - your web app; war 文件夹- 您的网络应用程序; contains static resources as well as compiled output包含静态资源以及编译输出

That means, that you need to put your StyleSheet.css file in the war folder.这意味着,您需要将StyleSheet.css文件放在war文件夹中。

After copying the StyleSheet.css file to the war folder (and fixing some typos in the code you've posted - see my edit) I got this as a proof that it works:StyleSheet.css文件复制到war文件夹(并修复您发布的代码中的一些拼写错误 - 请参阅我的编辑)后,我得到了它作为它工作的证明:

在此处输入图片说明

As further reading I suggest Public Path part of the Modules: Units of configuration documentation.作为进一步阅读,我建议模块的Public Path部分:配置文档的单元


After saying all the above: it would be much easier if you simply create a static html file in the war folder and open a new window with a link to this file as a parameter.在说完以上所有内容之后:如果您只需在war文件夹中创建一个静态 html 文件并打开一个新窗口,并以该文件的链接作为参数,那就容易多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM