如何以编程方式在HTML文件中添加部分外部文件?

时间:2014-04-22 10:05:13

标签: html html5

表的主体内容由java代码生成并将其导出到文件中。我如何在html文件的表定义中以编程方式读取它?

Java程序生成以下代码

            <tr>
                <th>Filename</th>
                <th>Type</th>
                <th>Date</th>
            </tr>
            <tr>
                <td>The Hitch Hiker's Guide to the Galaxy</td>
                <td>Book</td>
                <td>28/01/2011</td>
            </tr>
            <tr>
                <td>The Hitch Hiker's Guide to the Galaxy</td>
                <td>Film</td>
                <td>28/01/2011</td>
            </tr>
            <tr>
                <td>Dirk Gently's Holistic Detective Agency</td>
                <td>TV Series</td>
                <td>28/01/2011</td>
            </tr>

<div id="liveFilter">
    <div class="liveFilterContainer">
        <input type="text" class="liveFilterInput default" value="Live Filter" />
        <a href="#" class="clearField" title="Clear Filter">x</a>
    </div>
    <div class="noResults"><strong>Sorry.</strong> There is no match for your filter; please try again.</div>
    <table class="liveFilterList" border="0">
        <tbody>


 <!-- Java Genrated code goes here-->

        </tbody>
    </table>
</div>

2 个答案:

答案 0 :(得分:0)

您可以使用 jsp:include 将HTML代码包含到java文件中,

<jsp:include page="table.html" flush="true" />

包含文件必须指定为&#34;相对路径&#34;

相对网址看起来像路径名。它不能包含协议名称,端口号或域名。 URL可以是绝对的或相对于当前JSP页面。如果它是绝对的(以/开头),则路径名由Web或应用程序服务器解析。 你不能做到以下几点:

 <jsp:include page="http://www.cnn.com/todayNews.html">

<强> 更新 您可以使用jquery ajax包含 table.html 页面,

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js" ></script>
<script type="text/javascript"> 
    $('#btnload').click(function () { 
        $.ajax({
            context: this,
            dataType : "html",
            url : "table.html",
            success : function(results) {
                $(this).html(results);
            }
        });
    }); 
</script> 
</head>
<body>
    <button name="btnload" id="btnload" value="load code" />
</body>
</html>

答案 1 :(得分:0)

你最好的选择是iframe(感谢user1153551 - AJAX也不错,但需要额外的编程)。

另一个选择是根据存储的数据和模板生成整个演示文稿HTML。