簡體   English   中英

將多行的詳細信息從JSP傳遞到Servlet

[英]Pass the details of Multiple rows from JSP to Servlet

我的JSP文件中有一個“員工列表”,並且為每個員工提供了一個更新位置的選項(下拉列表)。 結果將傳遞到“ SaveTubeDetails” servlet,該servlet負責保存數據。

這是我當前正在使用的代碼:

<form action="SaveTubeDetails" method="POST">
 <table>
 <% 
   for(Iterator<Employee> itr = employeeArr.iterator(); itr.hasNext();){ 
   Employee emp= itr.next();
 %> 

<tr> 
  <td>
  <select name="location">
  <% for(int count=0; count<locArr.size(); count++){ %>     
  <option value="<%= locArr.get(count) %>" ><%= locArr.get(count) %></option>   
  <%} %>
  </select>
  </td> 
</tr>
</table>
</form>

我的問題是,如何將其保存為可傳遞到Servlet的員工列表?

注意:我沒有使用Spring和Hibernate。 而且我知道不應該使用scriptlet,但是在更新現有代碼時我別無選擇。 盡管這里只顯示了Employee的Location詳細信息,但實際上還有更多列,例如Age,DOB等。

在您的特定示例中,所有提交的值都是HttpServletRequest#getParameterValues()可用的字符串數組,其順序與輸入出現在HTML標記中的順序完全相同。

String[] location = request.getParameterValues("location");
// ...

作為替代,您可能要考慮在輸入名稱中包括員工ID,以便避免任何潛在的種族條件(例如,在另一位最終用戶在顯示表單和處理表單之間將雇員從數據庫中刪除時)提交):

<select name="location_<%=emp.getId()%>">

然后可以根據現有員工列表將其收集到servlet中,如下所示:

List<Employee> employees = employeeService.list();

for (Employee employee : employees) {
    String location = request.getParameter("location_" + employee.getId());
    // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM