一尘不染

如何在jsp中显示从dao获得的数据

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;
    }

}

阅读 348

收藏
2020-06-08

共1个答案

一尘不染

我发现了几个错误:

这里,

<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">

流程是错误的。您应该 每个循环中打印一个新行。交换他们。

<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注入攻击敏感,并且正在泄漏资源。修复它。

2020-06-08