如何使用c:foreach和arraylist?

zlwx9yxi  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(297)

选择进程.jsp

<p>out.print("list:"+${departmentList }); </p>

<c:forEach items="${departmentList } " var="department ">
    <p>${department.getCode() }   ${department.getDep_name() }</p>
</c:forEach>

输出<<如何打印部门列表的代码和部门名称??
foreach没有指纹

out.print("list:"+[department.DepartmentVO@722e831, department.DepartmentVO@408834bf, department.DepartmentVO@418561dd]);

控制器.java

// search DepartmentList
    @RequestMapping("/getDepartmentList.do")
    public ModelAndView getDepartmentList(DepartmentVO vo, DepartmentDAO departmentDAO, ModelAndView mav) {
        mav.addObject("departmentList", departmentDAO.getDepartmentList(vo)); // Model set
        mav.setViewName("selectProcess"); // View set
        return mav;
    }

部门dao.java

// search departmentList
    public List<DepartmentVO> getDepartmentList(DepartmentVO vo) {
        System.out.println("===> JDBC to getDepartmentList() ");
        List<DepartmentVO> departmentList = new ArrayList<DepartmentVO>();
        try {
            conn = JDBCUtil.getConnection();
            stmt = conn.prepareStatement(DEPARTMENT_LIST);  // => DEPARTMENT_LIST = "select * from department "
            rs = stmt.executeQuery();
            while (rs.next()) {
                DepartmentVO department = new DepartmentVO();
                department.setCode(rs.getInt("CODE"));
                department.setDep_name(rs.getString("DEP_NAME"));
                departmentList.add(department);
                System.out.println("=====>>>>>>{"+department.getCode() +"}");
                System.out.println("=====>>>>>>{"+department.getDep_name() +"}");
            }
            System.out.println("===> JDBC to getDepartmentList() done");
            for(DepartmentVO i : departmentList) { 
                System.out.println(i.getCode());
                System.out.println(i.getDep_name());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, stmt, conn);
        }
        return departmentList;
    }

输出

===> JDBC to getDepartmentList()
=====>>>>>>{1}
=====>>>>>>{1_name}
=====>>>>>>{2}
=====>>>>>>{2_name}
=====>>>>>>{3}
=====>>>>>>{3_name}
===> JDBC to getDepartmentList() done
1
1_name
2
2_name
3
3_name

部门VO.java

//VO(Value Object)
public class DepartmentVO {
    private int code;
    private String dep_name;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDep_name() {
        return dep_name;
    }

    public void setDep_name(String dep_name) {
        this.dep_name = dep_name;
    }
}

在里面

public DepartmentVO getDepartment(DepartmentVO vo) {
        System.out.println("===> JDBC To getDepartment() ");
        DepartmentVO department = null;
        try {
            conn = JDBCUtil.getConnection();
            stmt = conn.prepareStatement(DEPARTMENT_LIST);  // => DEPARTMENT_LIST = "select * from department "

            rs = stmt.executeQuery();
            if (rs.next()) {
                department = new DepartmentVO();
                department.setCode(rs.getInt("CODE"));
                department.setDep_name(rs.getString("DEP_NAME"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, stmt, conn);
        }
        return department;
    }

in controller

    @RequestMapping("/getDepartment.do")
    public ModelAndView getDepartment(DepartmentVO vo, DepartmentDAO departmentDAO, ModelAndView mav) {
        mav.addObject("department", departmentDAO.getDepartment(vo));
        mav.setViewName("selectProcess");

        return mav;
    }

in selectProcess.jsp
                        <c:forEach items="${department }" var="department ">
                                <p>${department.Code }   ${department.Dep_name }</p>
                        </c:forEach>

output  ==> it is print one . i know it 
    1 1_name

我不知道如何用c:foreach打印arraylist。
不是arraylist,只是打印department.code->好的打印
但是arraylist,department.getcode(),department.code->nothing只是空白
我怎么能??

gdx19jrr

gdx19jrr1#

它在打字后工作 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 并删除的items和var属性中的空格 <c:forEach> 不断变化 ${department.getCode()}${department.code}

相关问题