简体   繁体   English

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

[英]how to display data obtained from dao in jsp

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> 

in dao 在岛

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

}

I spot several mistakes: 我发现了几个错误:

Here, 这里,

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

You're overriding the list value with the value of the list item everytime. 您每次都用列表项的值覆盖列表值。 Don't do that. 不要那样做 Give the var an unique variable name. var一个唯一的变量名。 The entity name is the most straightforward choice. 实体名称是最直接的选择。

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

Note that I'd personally also rename the nothing-saying resultList to a more self-explaining projects . 请注意,我个人也将不说任何内容的resultList重命名为一个更容易解释的projects


And here, 和这里,

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

the flow is wrong. 流程是错误的。 You should print a new row inside each loop. 您应该每个循环中打印一个新行。 Swap them. 交换他们。

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

And here, 和这里,

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

the property names must start with lowercase (and fix the item name to be the same as in var ). 属性名称必须以小写字母开头(并将项目名称固定为与var相同)。

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

Note that I'd personally also get rid of proj prefix in some property names. 注意,我个人也将摆脱某些属性名称中的proj前缀。


And finally you're forgotten a closing </c:forEach> . 最后,您忘了结束语</c:forEach>

    </tr>
</c:forEach>

Unrelated to the concrete problem, your JDBC code is sensitive to SQL injection attacks and is leaking resources. 与具体问题无关,您的JDBC代码对SQL注入攻击敏感,并且正在泄漏资源。 Fix it as well. 修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM