如何从使用setAttribute()设置为bean的数组列表中获取值?

时间:2017-07-04 06:11:54

标签: jsp servlets struts-1

这是使用setAttribute

设置数组列表的代码
@Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        PrintWriter out;
        out = response.getWriter();
        dbc.openConnection();
        travelDetails = new java.util.ArrayList();
        HttpSession session = request.getSession(true);
        if ("OK".equals(session.getAttribute("login"))) {
            String sValue = String.valueOf(session.getAttribute("status"));
            String employeeId = String.valueOf(session.getAttribute("employeeId"));
            TravelSettlement ts = (TravelSettlement) form;
            TravelBean tBean;
            String travelId = "";
            travelId = ts.getCheckedValue()[0];
            session.setAttribute("travelSId", travelId);
            dateDetails = new java.util.ArrayList();
            travelId1 = new java.util.ArrayList();
            String query1 = "select td.travel_id,td.date,td.mode,td.travel_from,td.travel_to,td.purpose from travel_details td,travel_master tm where td.travel_id=tm.travel_id and td.travel_id='" + travelId + "'";
            ResultSet rs1 = dbc.processSelect(query1);
            while (rs1.next()) {
               String date = "";
               String mode = "";
               String travel_from = "";
               String travel_to = "";
               String purpose = "";
               tBean = new TravelBean();
               TravelBean tb1 = new TravelBean();
               long travel_Id = Long.valueOf(rs1.getString(1));
               tBean.setTravelId(travel_Id);
               Bean.setDate(DateUtil.stringToSQLDateFormat(rs1.getString(2), "yyyy-MM-dd", "dd-MM-yyyy"));
               tBean.setMode(rs1.getString(3));
               tBean.setTravelFrom(rs1.getString(4));
               tBean.setTravelTo(rs1.getString(5));
               tBean.setPurpose(rs1.getString(6));
               travelDetails.add(tBean);
               dateDetails.add(tBean);
               travelId1.add(travelId);
            }
            request.setAttribute("travelId", travelId1);
            request.setAttribute("dateDetails", dateDetails);
            session.setAttribute("travelDetails", travelDetails);
            return mapping.findForward("edit_request");
        } else {
        return mapping.findForward("error");
    }

}

这是我的JSP代码,访问bean数组数据

<html>
<body>
    <table width="100%" cellpadding="0" cellspacing="0" align="center" border="0" id="myTable">
    <script>
       var i = 0;
       var name="#tbodyId"+i;
    </script>
    <logic:iterate id="dateDetails" name="dateDetails">
       <tbody>
           <tr>
              <logic:lessThan property="date" name="dateDetails" value="2">
               <td width="14%" >
                 <html:text property="date" name="dateDetails" size="12" readonly="false" styleClass="hometext" />&nbsp;
                 <img src="./images/cal.gif" alt="calender" onclick="displayCalendar(document.forms[0].date[0], 'dd-mm-yyyy', this)"/>
               </td>
             </logic:lessThan>
             <logic:greaterEqual property="date" name="dateDetails" value="1">
              <td width="12%" >
                 <html:text property="date" name="dateDetails" size="12" readonly="false" styleClass="hometext" />&nbsp;
                 <img src="./images/cal.gif" alt="calender" onclick="displayCalendar(document.forms[0].date[i++], 'dd-mm-yyyy', this)"/>
              </td>
             </logic:greaterEqual>
               <td width="12%" class="tbldata" >
                 <html:select property="mode" styleClass="hometext" name="dateDetails">
                   <html:options name="modeOption" labelName="modeValue"/>
                 </html:select>
                </td>
                <td width="15%" class="tbldata" align="center"><html:text property="travelFrom" name="dateDetails"/></td>
                <td width="13%" class="tbldata" align="center"><html:text property="travelTo" name="dateDetails"/></td>
                <td class="tbldata">
                   <html:textarea property="purpose" name="dateDetails" rows="2" cols="20" styleClass="textarea" onchange="ajaxDateCheckValue()"/>
                </td>
                <td width="15%" class="tbldata" align="center">
                   <button type="button" onclick="setHidden()">Update</button>
                </td>
             </tr>
           </tbody>
        </logic:iterate>
     </table>
</body>
 </html>

我想使用该数组将用户编辑的详细信息更新到数据库,数组名称为(dateDetails)。

0 个答案:

没有答案