[英]Java - render HTML, JS and CSS and send output html as a string
[英]Render HTML with CSS in Java
我正在嘗試在我的Java應用程序中顯示HTML,該應用程序在我的HTML中包含鏈接的樣式表。
我正在使用Java中的XSLT將我的XML轉換為HTML。 我想要包含樣式表,以便我可以輕松設置html輸出的樣式。 但是樣式表被忽略,html正常輸出。
為此,我使用的是JEditorPane和HTMLEditorKit。 我在Dev Daily上找到了一些示例代碼來執行此操作。
我的樣式表坐在我的本地硬盤上,我想知道是否有人知道我如何使用它?
我有以下代碼:
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setEditable( false );
HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);
try {
kit.getStyleSheet().importStyleSheet( new URL( "file://D:\\mycssfile.css" ) );
} catch( MalformedURLException ex ) {
}
Document doc = kit.createDefaultDocument();
jEditorPane.setDocument(doc);
jEditorPane.setText(html);
在我從xsl輸出的html中,使用以下內容鏈接css - 我得到包含或排除的相同結果:
<link rel="stylesheet" type="text/css" href="mycss.css" />
有任何想法嗎?
干杯,
Andez
您的網址無效,因此無法找到您的CSS文件。 將其更改為:
kit.getStyleSheet().importStyleSheet(new URL("file:///D:/mycssfile.css"));
或者更好的是,不是使用URL,而是將css文件添加到類路徑中,然后將其作為資源加載,如下所示:
kit.getStyleSheet().importStyleSheet(MyClassName.class.getResource("mycssfile.css"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.