2017-05-24 40 views
0

我試圖從具有多行的表中選擇一行,並在另一個JSP中顯示所選行的值。從一個JSP中的表中獲取選定行的值到另一個JSP

但無論選擇哪一行,我只會得到第一行的值。從第一個JSP中選擇的表格數據從.js文件填充。

下面是代碼從我第一次JSP代碼段:

<div> 
<form class="form-inline" name = "select_req" action="/Main.do" method = post> 
    <table class="table" id = "searchresult"> 
     <tr> 
      <th>From</th> 
      <th>To</th> 
      <th>Airline</th> 
      <th>Fare</th> 
      <th>Action</th> 
     </tr> 
     <tr ng-repeat="flight in flights | filter:query"> 
      <td><input value = "{{flight.from}}" name = "from"></td> 
      <td><input value = "{{flight.to}}" name = "to"></td> 
      <td><input value = "{{flight.airline}}" name = "airline"></td> 
      <td><input value = "{{flight.fare}}" name = "fare"></td> 
      <td><input type="submit" value="Select" name = "select_req" class="btn btn-success"/></td> 
     </tr> 
    </table> 
    <input type="hidden"/> 
</form> 
</div> 

這是它發送到另一個JSP,servlet代碼:

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String from = request.getParameter("from");   
    String to = request.getParameter("to");   
    String airline = request.getParameter("airline");  
    String fare = request.getParameter("fare"); 
    HttpSession session = request.getSession(); 
    session.setAttribute("from", from); 
    session.setAttribute("to", to); 
    session.setAttribute("airline", airline); 
    session.setAttribute("fare", fare); 

    request.getRequestDispatcher("/WEB-INF/views/Payment.jsp").forward(request, response); 
} 

最後的JSP是從選定的行表需要顯示:

<table class = "table"> 
      <tr> 
       <th>From</th> 
       <th>To</th> 
       <th>Airline</th> 

       <th>Fare</th> 
      </tr> 
      <tr> 
       <td><%= session.getAttribute("from") %></td> 
       <td><%= session.getAttribute("to") %></td> 
       <td><%= session.getAttribute("airline") %></td> 
       <td>$<%= session.getAttribute("fare") %></td> 
      </tr> 
     </table> 

enter image description here

回答

0

表單元素包含所有錶行的所有輸入元素。提交表單時,它會將所有這些輸入元素的值發送到服務器。

現在在服務器上所有參數都是多值的。如果您致電ServletRequest.getParameter(String)它將返回第一個值 - 它對應於您的第一個表格行中的輸入。

通過調用request.getParameterValues("from")進行檢查 - 它應該返回一個數組,其長度等於表中的行數,包含第一個表列的所有輸入。

你可能想要的只是發送包含按下按鈕的行的值。要麼使用Javascript來完成此操作,要麼將每個<tr>包裝在自己的<form>元素中。

+0

我如何發送選定的行只使用JavaScript或將每個包裝在自己的

元素中。 – kms

相關問題