如何在jsp中显示从dao获得的数据[英] how to display data obtained from dao in jsp

问题描述

在jsp中

<table width="100%" id='table1'  border="0" cellspacing="2" cellpadding="2">
    <tr class="tab-highlighted-2">
        <td class="tab-highlighted-2" width="10%">
          <div align="left">Project ID</div>
        </td>
        <td class="tab-highlighted-2" width="10%">
          <div align="left">Project Name</div>
        </td>
        <td class="tab-highlighted-2" width="20%">
          <div align="left">Cost Center</div>
        </td>
        <td class="tab-highlighted-2" width="20%">
          <div align="left">Manager</div>
        </td>
    </tr>
  <tr class="bg-row1">
  <c:forEach items="${resultList}" var="resultList"> 
        <td class="td-highlighted-2">
         <div align="left"><a href="UpdateProject.html">${resultList.Projid}</a></div>
        </td>
        <td class="td-highlighted-2">
          <div align="left">${resultList.Projname}</div>
        </td>
        <td class="td-highlighted-2">
          <div align="left">${resultList.Cost}</div>
        </td>
        <td class="td-highlighted-2">
          <div align="left">${resultList.Manager}</div>
        </td>  
    </tr>  
    </table> 

在道

public final class SearchProjDAO   
{ 
    private static InitialContext context;  
    String CLASS_NAME="DBConnectionFactory";  

    public ArrayList  submitProjectDetails(SearchProjVO searchprojVO)   
    {  
        ArrayList ar = new ArrayList();
        String methodname="createConnection";
        Connection conn  = null;
        PreparedStatement psmt;
        try {
            System.out.println("in DAO");
            System.out.println("searchprojVO id=="+searchprojVO.getProjid());

            conn = DBConnection.getJNDIConnection();
            ResultSet rs = null;
            String query="select * from CR_EMPLOYEE_DETAILS";if(searchprojVO.getProjid()!=null || searchprojVO.getProjname()!=null || searchprojVO.getManager()!=null)
            query=query+" where ";
        if(searchprojVO.getProjid()!=null)
            query=query+" PROJ_ID="+searchprojVO.getProjid();
        if(searchprojVO.getProjname()!=null)
            query=query+"PROJ_NAME="+searchprojVO.getProjname();
        if(searchprojVO.getCost()!=null);
            query=query+"PROJ_COST="+searchprojVO.getCost();
        if(searchprojVO.getManager()!=null)
            query=query+"PROJ_MANAGER="+searchprojVO.getManager();

            psmt= conn.prepareStatement(query);
            rs=psmt.executeQuery();

            while(rs.next())
            {
                SearchProjVO projVO = new SearchProjVO();
                projVO.setProjid(rs.getString("PROJ_ID"));
                projVO.setManager(rs.getString("PROJ_NAME"));
                projVO.setProjname(rs.getString("PROJ_COST"));
                projVO.setManager(rs.getString("PROJ_MANAGER"));
             ar.add(projVO);
                                }

            System.out.println("conn==="+conn); 
        } catch (Exception e) {
            e.printStackTrace(System.err);
        } 

        return ar;
    }

}

推荐答案

我发现了几个错误:

这里,

<c:forEach items="${resultList}" var="resultList"> 

您每次都用列表项的值覆盖列表值.不要那样做.给 var 一个唯一的变量名.实体名称是最直接的选择.

<c:forEach items="${resultList}" var="project"> 

请注意,我个人也会将无话可说的 resultList 重命名为更易于解释的 projects.

<小时>

这里,

<tr class="bg-row1">
    <c:forEach items="${resultList}" var="project"> 

流程错误.您应该在每个循环inside 打印一个新行.交换它们.

<c:forEach items="${resultList}" var="project"> 
    <tr class="bg-row1">
<小时>

这里,

${resultList.Projid}
${resultList.Projname}
${resultList.Cost}
${resultList.Manager}

属性名称必须以小写开头(并固定项目名称与var中的相同).

${project.projid}
${project.projname}
${project.cost}
${project.manager}

请注意,我个人也会去掉某些属性名称中的 proj 前缀.

<小时>

最后你忘记了结束 </c:forEach>.

    </tr>
</c:forEach>
<小时>

与具体问题无关,您的 JDBC 代码对 SQL 注入攻击很敏感,并且正在泄漏资源.也修复它.

本文地址:https://www.itbaoku.cn/post/978645.html