将整数从jsp页传递到控制器

时间:2019-02-06 10:56:35

标签: java jsp servlets

如何将“ i”的值作为可用列表的索引从jsp页面传递到控制器?我和availableRooms在给定的代码中。

我想获取表行的索引,在其中按下“ addRoom”按钮作为控制器的输入,在该控制器中,我试图将传递的索引处的对象添加到另一个列表中,并显示在保留购物车中。 我在控制器中尝试了这一行代码

int index = Integer.parseInt(request.getParameter("roomPosition");

但是我每次都得到索引为0

AvailableRooms.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="main.bean.HotelBean" %>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rooms Available</title>
</head>
<body>
<center>
<h3>Rooms Available</h3>
</center>
<form name="availableRooms" action="HotelReservationContoller" method="post">
<table border="1" align="center">
    <thead bgcolor="#FDE6B5">
        <font color="white">
        <tr>
            <td>Room Number</td>
            <td>Room Type</td>
            <td>Index</td>
            <td></td>
        </tr>
        </font>
    </thead>
    <%
      HotelBean rb;
        HttpSession newSession=request.getSession(false);
        ArrayList<HotelBean> availableRooms = (ArrayList<HotelBean>)session.getAttribute("availableRooms");
        Iterator<HotelBean> HotelBeanIterator = availableRooms.iterator();

        int i=0;
        while (HotelBeanIterator.hasNext())
        {   
             rb = (HotelBean) HotelBeanIterator.next();

              %>
        <tr>    
            <td><%= rb.getRoomNo()%></td>
            <td><%= rb.getRoomType()%></td>
            <td><%= i %>
            <td><input type="hidden" value="<%= i %>" name="roomPosition"/> <input type="submit" value="addRoom" name="button"></td> 
        </tr>
    <%
        i++;
        }
    %>  

</table>

 <%
if(newSession!=null)
{
    ArrayList<HotelBean> pickedRooms=null;

    pickedRooms=(ArrayList<HotelBean>)newSession.getAttribute("ReservationCart");

        newSession.setAttribute("ReservationCart",pickedRooms);
    %>
    <jsp:include page="ReserveCart.jsp" flush="true"/>
    <%}%> 
</form>

</body>
</html>

0 个答案:

没有答案