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

查看:236
本文介绍了如何在jsp中显示从dao获得的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}


推荐答案

我发现了几个错误:

此处,

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

您每次都会使用列表项的值覆盖列表值。不要那样做。为 var 提供唯一的变量名称。实体名称是最直接的选择。

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. The entity name is the most straightforward choice.

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

请注意,我个人也会重命名无所谓 resultList 更自我解释项目

Note that I'd personally also rename the nothing-saying resultList to a more self-explaining projects.

此处,

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

属性名称必须以小写字母开头(并将项目名称修改为与在 var )。

the property names must start with lowercase (and fix the item name to be the same as in var).

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

请注意,我个人也会在某些属性名称中删除 proj 前缀。

最后你忘记了收盘< / c:forEach>

    </tr>
</c:forEach>






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


Unrelated to the concrete problem, your JDBC code is sensitive to SQL injection attacks and is leaking resources. Fix it as well.

这篇关于如何在jsp中显示从dao获得的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆