簡體   English   中英

如何在 GWT 中將 CSS 文件鏈接到 HTML

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

在客戶端的 GWT 應用程序中,我生成一個包含 HTML 內容的字符串,並將其傳遞給在新選項卡中打開它的函數。 我已經編寫了一個 CSS 文件來設置 HTML 內容的樣式並給出了一個鏈接。 但是我的 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 文件 - StyleSheet.css

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

那么,什么是問題,我哪里出錯了?

你寫了 html..append(""); 而不是 html.append();

它是否回答了您的問題?

在您的評論之一中,您寫道:

這兩個文件都存在於同一個文件夾中。 所以,我想路徑是正確的。

這不是真的。 你需要從公用文件夾區分文件夾。 如果您可以鏈接文件夾中的文件並使其可供下載,那將是非常不安全的。

您應該閱讀有關標准目錄和包布局的信息 在那里你會發現:

src 文件夾- 包含生產 Java 源代碼

war 文件夾- 您的網絡應用程序; 包含靜態資源以及編譯輸出

這意味着,您需要將StyleSheet.css文件放在war文件夾中。

StyleSheet.css文件復制到war文件夾(並修復您發布的代碼中的一些拼寫錯誤 - 請參閱我的編輯)后,我得到了它作為它工作的證明:

在此處輸入圖片說明

作為進一步閱讀,我建議模塊的Public Path部分:配置文檔的單元


在說完以上所有內容之后:如果您只需在war文件夾中創建一個靜態 html 文件並打開一個新窗口,並以該文件的鏈接作為參數,那就容易多了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM