在客户端的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%;
}
那么,问题出在哪里,我错在哪里呢?
发布于 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文件,并打开一个新窗口并将指向此文件的链接作为参数,则会容易得多。
https://stackoverflow.com/questions/56556903
复制相似问题