gpt4 book ai didi

java - 下拉列表空指针 JSP 到 Servlet

转载 作者:行者123 更新时间:2023-11-29 23:15:48 25 4
gpt4 key购买 nike

我在从 jsp 获取下拉列表中的值时遇到困难。我得到的字符串为空值,仅是“”。但如果我只使用部门的文本框它就会起作用。我很难找到我的代码中的错误在哪里。

这是我的 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 的:

私有(private)部门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 类中 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 的有用链接.

关于java - 下拉列表空指针 JSP 到 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27824048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com