how to use c:forEach with arrayList?

379 views Asked by At

selectProcess.jsp

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

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

output << how to print departmentList's code and dep_name ??

there is no print at forEach

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

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

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

output

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

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

in

    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

i don't know how to print arrayList with c:forEach.

not arrayList, Just print department.code -> good print

but arrayList, department.getCode() , department.code -> nothing just blank

how can i ??

1

There are 1 answers

0
Hime On

it's working after typing <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> at the top and removing the spaces in the items and var attributes of <c:forEach> and changing ${department.getCode()} to ${department.code}