下拉列表空指针 JSP 到 Servlet

Drop Down list Null Pointer JSP to Servlet

我很难从 jsp 获取下拉列表中的值。我得到 String 的 Null 值,它只是 ""。但是,如果我只为部门使用文本框,它将起作用。我很难找到我的代码中的错误在哪里。

这是我的 JSP 文件:

 <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>

        </head>
        <body>

            <form method="POST" action='PersonnelController' name="frmAddData" class="form">

                <fieldset>
                    <legend id="myLegend">User</legend>

                    <label for="firstname">First Name :</label>
                            <input type="text" name="firstname"
                                value="<c:out value="${data.firstname}" />" /><br />

                        <label>Last Name :</label>
                            <input type="text" name="lastname"
                                value="<c:out value="${data.lastname}" />" /><br />

                        <!--<label>Department Name :</label>
                            <input type="text" name="department_id" 
                            value="<c:out value="${data.department_id}" />" /><br />-->

                        <label>Department Name :</label>
                <select name="personnel">
                <c:forEach items="${personnels}" var="personnel">
                <option value="${personnel.department_id}"><c:out value="${personnel.department_name}" /></option>
                </c:forEach>
</select>
                </fieldset>
                <input type="submit" value="Submit" class="submit"/>
            </form>

        </body>
        </html>

这是针对 Servlet 的:

私人部门Dao dd;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<DepartmentBean> personnels = dd.getAllDepartmentName();
    request.setAttribute("personnels", personnels);
    request.getRequestDispatcher("/WEB-INF/personnel.jsp").forward(request, response);
}

}

这是我的 DAO:

   public List<DepartmentBean> getAllDepartmentName() {
                List<DepartmentBean> departments = new ArrayList<DepartmentBean>();
                try {
                    Statement statement = connection.createStatement();
                    ResultSet rs = statement.executeQuery("select department_name,id from department ORDER BY department_name");
                    while (rs.next()) {
                        DepartmentBean department = new DepartmentBean();
                        department.setPeople_manager_name(rs.getString("department_name"));
                        departments.add(department);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                return departments;

问题是您没有在 dao class 中的 DepartmentBean 对象引用中填写必要的字段。您只填写了 people_manager_name 字段,而在 JSP 中您正在请求 department_iddepartment_name 的数据,这将调用 DepartmentBean#getDepartment_id()DepartmentBean#getDepartment_name() 方法。

getAllDepartmentName 方法中更改:

while (rs.next()) {
    DepartmentBean department = new DepartmentBean();
    department.setDepartment_name(rs.getString("department_name"));
    department.setDepartment_id(rs.getInt("department_id"));
    departments.add(department);
}

这是关于 how expression language works 的有用 link。