我是新來的java,並試圖將HTML/CSS和Javascript嵌入到java代碼中。 任何指針將有助於完成構建我的代碼。我使用Eclipse編輯器編寫我的Java代碼。在使用IDE的java代碼中使用HTML,CSS和Javascript Eclipse
我已經能夠在我的java代碼中成功顯示HTML顯示的一些部分,但我試圖通過html/css/javascript實現表格排序,並且希望將其添加到我的java代碼中。
下面是我的display.java文件
public void transformForRequestSuccessful(MarkupOutput out) {
//Call render methods
renderdisplay(out);
}
public void renderdisplay(MarkupOutput out)
{
out.append("<BR/>");
out.append("<p>Look at the body!</p>");
out.append("<BR/>");
}
下面是HTML/CSS/JavaScript代碼可以被粘貼的值並運行良好。
我把這個HTML文件保存爲sort.html,獨立調用的時候工作正常。
html>
<head>
<meta http-equiv="content-type" content="text/html;charset=Windows-1252">
<script type="text/javascript">
var people, asc1 = 1,
asc2 = 1,
asc3 = 1;
window.onload = function() {
people = document.getElementById("people");
}
function sort_table(tbody, col, asc) {
var rows = tbody.rows,
rlen = rows.length,
arr = new Array(),
i, j, cells, clen;
// fill the array with values from the table
for (i = 0; i < rlen; i++) {
cells = rows[i].cells;
clen = cells.length;
arr[i] = new Array();
for (j = 0; j < clen; j++) {
arr[i][j] = cells[j].innerHTML;
}
}
// sort the array by the specified column number (col) and order
(asc)
arr.sort(function (a, b) {
return (a[col] == b[col]) ? 0 : ((a[col] > b[col]) ? asc : -1 *
asc);
});
// replace existing rows with new rows created from the sorted array
for (i = 0; i < rlen; i++) {
rows[i].innerHTML = "<td>" + arr[i].join("</td><td>") + "</td>";
}
}
</script>
<style type="text/css">
table {
border-collapse: collapse;
border: none;
}
th,
td {
border: 1px solid black;
padding: 4px 16px;
font-family: Times New Roman;
font-size: 15px;
text-align: left;
}
th {
background-color: #C8C8C8;
cursor: pointer;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th onclick="sort_table(people, 0, asc1); asc1 *= -1; asc2 = 1;
asc3 = 1;">ServerName</th>
<th onclick="sort_table(people, 1, asc2); asc2 *= -1; asc3 = 1;
asc1 = 1;">UserName</th>
<th onclick="sort_table(people, 2, asc3); asc3 *= -1; asc1 = 1;
asc2 = 1;">JobId</th>
</tr>
</thead>
<tbody id="people">
<tr>
<td>Server1</td>
<td>ABC</td>
<td>18</td>
</tr>
<tr>
<td>Server2</td>
<td>XYZ</td>
<td>20</td>
</tr>
</tbody>
</table>
</body>
</html>
任何幫助或建議如何將其嵌入到java代碼將是有益的。 我很抱歉,如果我聽起來天真或不知道基本的Java編程。
感謝
謝謝您的意見。我正在使用asp.net。你可以請示例爲ASP示例? – user3438791
但你要求提供java代碼。你怎麼能在asp.net中嵌入java代碼?請澄清你的問題 –
我的意思是我使用IIS作爲我的網絡服務器。我試圖實現的是類似於這篇文章https://stackoverflow.com/questions/15842683/string-builder-int-html-table-format但我無法esc在我的CSS和JS中使用的特殊字符。所以需要幫助。 – user3438791