如何在jsp页面上显示ModelMap属性的数据?

时间:2014-04-04 10:22:04

标签: spring spring-mvc

我想在数据库中显示一些细节到查看页面,所以我尝试将查询返回的结果放在List上,但它是show detils,如:

如果我在数据库中有10个细节,那么它会显示10个细节。

查看页面详细信息:

<c:choose>
        <c:when test="${not empty dtoBean}">
            <c:forEach items="${dtoBean}" var="upComLeave">
                <tr>
                    <td><span>${upComLeave.employee_id}</span></td>
                    <td><span>${upComLeave.no_of_leave}</span></td>
                    <td><span>${upComLeave.fromDate} - ${upComLeave.toDate}</span></td>
                    <td><span>${upComLeave.leaveType}</span></td>
                    <td><span>${upComLeave.leavePeriod}</span></td>
                    <td><span>${upComLeave.appliedLeave}</span></td>
                    <td><span>${upComLeave.leaveStatus}</span></td>
                    <c:if test="${upComLeave.leaveStatus ne 'Approved' }"> //check status if accepted, don't render Accept button
                        <td><button id="btnAccept" name="action" type="submit" value="Accept${upComLeave.emp_id}" onclick="">Approve</button></td>
                    </c:if>
                    <c:if test="${upComLeave.leaveStatus ne 'rejected' }">//check status if accepted, don't render Reject button
                    <td><button id="btnReject" name="action" type="submit" value="Reject${upComLeave.emp_id}">Reject</button></td>
                    </c:if>
                    <c:if test="${upComLeave.leaveStatus eq 'cancel' }">//check status if cancel, render cancel button
                    <td><button id="btnCancel" name="action" type="submit" value="Cancel${upComLeave.emp_id}">Cancel</button></td>
                    </c:if>
                </tr>
            </c:forEach>
        </c:when>
        <c:otherwise>
            upcomingLeavesList is empty or null..
        </c:otherwise>
      </c:choose>

控制器类:

 @RequestMapping(value = "/upcomingLeaves", method = RequestMethod.GET)
           public String showForm(@ModelAttribute("loginForm")EmpRegistrationForm profileForm, BindingResult result , HttpServletRequest request, HttpServletResponse response, ModelMap model){ 


             try{           
                    HttpSession session=super.getSession(model);
                    CommonDTOBean dtoBean=(CommonDTOBean)session.getAttribute("dtoBean");
                    List upcomingLeavesList=LeaveStatusWorker.upcomingLeaves(dtoBean);
                    session.setAttribute("upcomingLeavesList", upcomingLeavesList);

model.addAttribute("dtoBean",dtoBean);

                    return GlobalConstants.UPCOMING_LEAVES;
                    }catch(Exception e){

                    }


             return "redirect: GlobalConstants.UPCOMING_LEAVES";    
           }

DTO服务类有以下方法从db获取详细信息:

public static  List<CommonDTOBean> upcomingLeaves(CommonDTOBean dtoBean1) throws SQLException{
    con=DBConnection.getConnection();
    query= "select e.emp_id,e.LeaveType,e.leavePeriod,e.fromdate,e.todate,e.leave_status ,er.no_of_leave,e.no_of_leaves from EMPLEAVE e,newemp_register er where e.emp_id=er.emp_id " ;
    PreparedStatement pstmt=con.prepareStatement(query);
    System.out.println( GlobalConstants.LEAVE_STATUS);
     rs=pstmt.executeQuery();

     List<CommonDTOBean> list=new ArrayList<CommonDTOBean>();

    while(rs.next()){


        dtoBean1.setEmployee_id(rs.getInt(1));
        dtoBean1.setLeaveType(rs.getString(2));
        dtoBean1.setLeavePeriod(rs.getString(3));
        dtoBean1.setFromDate(rs.getString(4));
        dtoBean1.setToDate(rs.getString(5));
        dtoBean1.setLeaveStatus(rs.getString(6));
        dtoBean1.setNo_of_leave(rs.getString(7));
        dtoBean1.setAppliedLeave(rs.getString(8));

        list.add(dtoBean1);

    }

    return list;
    }

表单bean类有:

public class CommonDTOBean {
private int employee_id;
    private String empName;
    private String email;
    private String designation;
    private String password;
    private String DOJ;
    private String empType;
    private String leaveStatus;
    ..
    ..
    ..
// all setters and getters
}

一切正确还是我做错了什么?

1 个答案:

答案 0 :(得分:0)

在这里,您传递一个对象并为每条记录更新相同内容并添加到列表中。这就是它显示相同结果的原因。而是像这样做

public static  List<CommonDTOBean> upcomingLeaves() throws SQLException{
    con=DBConnection.getConnection();
    query= "select e.emp_id,e.LeaveType,e.leavePeriod,e.fromdate,e.todate,e.leave_status ,er.no_of_leave,e.no_of_leaves from EMPLEAVE e,newemp_register er where e.emp_id=er.emp_id " ;
    PreparedStatement pstmt=con.prepareStatement(query);
    System.out.println( GlobalConstants.LEAVE_STATUS);
     rs=pstmt.executeQuery();

     List<CommonDTOBean> list=new ArrayList<CommonDTOBean>();

    while(rs.next()){

        CommonDTOBean dtoBean1 = new CommonDTOBean ();  
        dtoBean1.setEmployee_id(rs.getInt(1));
        dtoBean1.setLeaveType(rs.getString(2));
        dtoBean1.setLeavePeriod(rs.getString(3));
        dtoBean1.setFromDate(rs.getString(4));
        dtoBean1.setToDate(rs.getString(5));
        dtoBean1.setLeaveStatus(rs.getString(6));
        dtoBean1.setNo_of_leave(rs.getString(7));
        dtoBean1.setAppliedLeave(rs.getString(8));

        list.add(dtoBean1);

    }

    return list;
    }

每次获取的记录都应该有新的CommonDTOBean对象。