- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在jsp页面中有一个html表,动态从数据库加载数据,然后我想通过servlet将表中的所有数据保存到数据库中。现在我的问题是我有一个jsp页面,它显示来自servlet的数据表,其中带有复选框,我必须将检查的内容发送到servlet以更新到数据库,如何做到这一点。
提前致谢,这是我的 table
<form action="showKwh" method="POST">
<input type="submit" value="show"/>
<table id="adminTable" class="detailsTable">
<tr class="header">
<th colspan="4">Kilowat Entry</th>
</tr>
<tr class="tableHeading">
<td></td>
<td>customer id</td>
<td>name</td>
<td>group</td>
<td>kwh</td>
<td>kwd</td>
</tr>
<c:forEach var="cust" items="${customerKwh}" varStatus="iter">
<tr id="${cust.id}" class="${((iter.index % 2) == 1) ? 'lightBlue' : 'white'} tableRow">
<td><input type="checkbox" name="check1" class="checker" value="ON" /></td>
<td id="id?${customer.id}">${cust.id}</td>
<td >${cust.name}</td>
<td >${cust.type}</td>
<td >${cust.kwh}</td>
<td><input type="text" name="txt" size="8" id="kwd${cust.id}" value="${param.value}" class="name1" /></td>
</tr>
</c:forEach>
</table>
</form>
最佳答案
要么将提交时需要回发的所有数据放入表单字段中,然后浏览器发送它,要么使用一些 JavaScript(例如 jQuery)来操作 html 表的 DOM,在客户端提取数据并通过 ajax 请求发送要在服务器端解析的 JSON 或 XML 形式。
不过,这是相当奇怪的要求。由于表中的数据源自处理响应的同一服务器,因此使用一组行标识符进行响应就足够了,服务器将通过这些标识符识别完整的行数据。
您可以通过复选框的值传递这些标识符:<input type="checkbox" name="check1" class="checker" value="${cust.id}" />
。然后,遵循 HTML 标准,只有具有 checked
的复选框或checked="checked"
属性将包含在响应中。然后您的 servlet 可以处理所有选中的复选框并获取所有需要的标识符。
使用 jQuery 提取表单数据。首先,添加到您的 <td>
带有标记所包含数据的数据类,因此我们可以使用 jQuery 选择它:
<td class="customerName">${cust.name}</td>
<td class="customerType">${cust.type}</td>
...and so on.
使用元素 <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
将 jQuery 包含到 JSP 中添加在关闭标签之前。然后在包含我们的脚本的页面上添加另一个脚本元素。这是一个例子:
<html>
<head>
</head>
<body>
<form action="showKwh" method="POST">
<input type="submit" value="show"/>
<table id="adminTable" class="detailsTable">
<tr class="header">
<th colspan="4">Kilowat Entry</th>
</tr>
<tr class="tableHeading">
<td></td>
<td>customer id</td>
<td>name</td>
<td>group</td>
<td>kwh</td>
<td>kwd</td>
</tr>
<tr id="123" class="lightBlue tableRow">
<td><input type="checkbox" name="check1" class="checker" value="123"/></td>
<td id="id?123" class="customerId">123</td>
<td class="customerName">Ivan</td>
<td class="customerType">Person</td>
<td class="customerKWH">54321</td>
<td><input type="text" name="txt" size="8" id="kwd123" value="98765" class="name1"/></td>
</tr>
</table>
</form>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function () { //launch this code after the whole DOM is loaded
$("form").submit(function (event) { // function to process submitted table
var tableData = []; // we will store rows' data into this array
$("#adminTable") // select table by id
.find(".tableRow") // select rows by class
.has(":checked") // select only rows with checked checkboxes
.each(function () { // for each selected row extract data
var tableRow = {};
var jRow = $(this);
tableRow.customerId = jRow.find('td.customerId').text();
tableRow.customerType = jRow.find('td.customerType').text();
tableRow.customerKWH = jRow.find('td.customerKWH').text();
tableRow.costomerKWD = jRow.find('input.name1').val();
tableData.push(tableRow);
});
$.post(
"http://google.com", /*url of consuming servlet*/
{tableData: tableData}, /*data*/
function () {
alert("Success!");
}, /*function to execute in case of success*/
"json" /* data type */
);
event.preventDefault(); //Prevent sending form by browser
}
);
});
</script>
</body>
</html>
要在浏览器中处理表单提交的表值,您可以考虑以下方法。
HttpServletRequest 继承自 ServletRequest 方法 getParameterMap(),该方法返回 Map。 (http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getParameterMap())。您可以使用一些参数名称约定来解析它。例如:
Map<String, String[]> tableData = getParameterMap();
String[] idsToUpdate = tableData.get("selectedIds");
for (String id : idsToUpdate){
String kwdParamName = "kwd"+id;
String kwd = tableData.get(kwdParamName)[0];
}
无论如何,您应该以某种方式解析请求数据。两者(基于 JSON 和基于表单)各有利弊。您应该选择哪一种能够产生更干净、更强大的解决方案。也许,在审美上更让你满意。最后但并非最不重要的一点是,您的客户端上下文是什么:它是否启用了 javascript,它是单页应用程序还是往返应用程序。在单页中,通过 JSON 来回传递数据是更常见的方法。在往返中 - 也许基于表单会更实用。
关于java - 将 html 表从 jsp 页面传递到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27221934/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!