如何使用 JSTL 保持从另一个 JSP 页面传递的数据?

How to keep data passed from another JSP page using JSTL?

我想创建一个简单的基于 Web 的汽车预订应用程序,类似于此 page. In that page, you can choose your location, destination and also Date of reservation. After you click next, the page will redirect you to another page where you choose time you desire, like this。如果您单击任何时间按钮,它会显示一个对象(汽车),在同一页面中显示已预订或可用的座位信息。

我有一个包含唯一 ID、位置、目的地、日期、时间和可用座位的数据库。为了像上面的例子一样显示时间,我必须先确定位置、目的地和日期(因为不同的目的地和日期会有不同的时间)。为了获得剩余座位数的信息,我必须确定位置、目的地、日期和时间。

在我的应用程序中,我有 3 个 .jsp 页面,我们称它为 First.jsp(用户在此处输入位置、目的地和日期)Second.jsp(用户在 [=33 中填写的信息=]会传到这里,然后再传到third.jsp)Third.jsp(用户在这里输入时间,数据会传到second.jsp。整个数据都会在那里处理。而数据最终将传回 Third.jsp 向用户显示可用座位信息。

问题是,我可以很好地将数据从 first.jsp 传递到 third.jsp。但是在我从 third.jsp 检索数据并将其传回 second.jsp 之后。来自 first.jsp 的数据已经消失。这让我无法在那里处理所有数据。

我的整体代码是这样的:

First.jsp

<form action= second.jsp method= post>

<SELECT class: drop id: location name: locationlist>
   <option value = locationvalue/>
</SELECT>

<SELECT class: drop id: destination name: destinationlist>
   <option value = destinationvalue/>
</SELECT>

<SELECT class: drop id: date name: datelist>
   <option value = datevalue/>
</SELECT>

<input type= submit name=submitbtn/>
</form>

Second.jsp

<c:if test=(param.time is empty)>
   <c:redirect url: third.jsp>
       <c:param name= locationvalue value${param.locationlist>
   </c:redirect>
<c:if/>

<c:if test=(param is not empty>
   <s:query 
      SELECT id, seat FROM schedule WHERE location = 'locationlist' destination = 'destinationlist' date = 'datelist' time = 'timelist' // does not work here 
   </s:query>

Third.jsp

<s:query dataSource="${ds}" var="resultset">
        SELECT DISTINCT time FROM schedule WHERE location = '${param.locationvalue}'
</s:query>

<form action="second.jsp" method="post">
    <center>
        <c:forEach items="${resultset.rows}" var="row">
            <input type="submit" value="${row.time}" name="time">
        </c:forEach>
    </center>
</form>

在一般情况下,<input type="hidden"> 将用于此类应用程序(多步向导)。

因此,为了简单起见,您可以 "forward" 从 third.jspsecond.jsp 的输入,如下所示:

<form action="second.jsp">
    <input type="hidden" name="fieldname" value="value from first.jsp"/>
    <!--rest of the field in this form-->
</form>

当然,从 second.jsp 访问它就像:

${param.field}
${fn:escapeXml(param.field)} //escape xml in el way
<c:out value="${param.field}"></c:out> //escape xml by default

另一种方法是将每个字段存储到您的 session 对象中。 session 中的属性将持续存在,只要它没有失效或应用程序没有 clear/override 它。

<c:set scope="request" value="${param.field}" var="field"/>

根据您的上下文,会话管理可以以不同的方式完成。