这是我使用spring-mvc实现的。正如我所观察到的resultSet
在ChildNameAccess.java
中正常工作。当我在这里打印声明时,它会打印出我真正需要的选项。但下拉框重复结果集对象中的一个结果。任何人都可以跟踪这个并告诉我我的jsp有什么问题吗?下拉选项菜单重复相同的选项
controller.java
@RequestMapping(value="/my_children", method = RequestMethod.GET)
public void viewMyChild(Model model) {
ChildNameAccess childNameDAO = new ChildNameAccess();
try{
java.util.List<Child> children = childNameDAO.getChildDataByMotherId("M-2");
model.addAttribute("children",children);
System.out.println(children);
}
catch (SQLException e) {
e.printStackTrace();
}
}
my_children.jsp
<div class="container-fluid bg-2 text-center">
<form:form method="get" >
<div class="div_box">
<select>
<option value="top" >Select child</option>
<c:forEach items="${children}" var="children">
<option value="" >${children.firstName} ${children.lastName}</option>
</c:forEach>
</select>
<br>
<div align ="justify">
<button type="button" onclick="location.href='/web/mother/my_child_details'" class="btn btn-success active">View Details</button>
</div>
</div>
</form:form>
ChildNameAccess.java
package com.emidwife.web.models.dataAccess;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import com.emidwife.web.models.entities.Child;
import com.emidwife.web.models.utilities.Database;
public class ChildNameAccess {
private Database connection = new Database();
Child child = new Child();
public List<Child> getChildDataByMotherId(String motherID) throws SQLException {
connection.openConnection();
List<Child> children = new ArrayList<Child>();
try{
ResultSet resultSet = connection.getData("SELECT * FROM childdetails WHERE MotherID=\'" + motherID + "\'");
while(resultSet.next()){
System.out.println(resultSet.getString("FirstName"));
child.setChildId(resultSet.getString("ChildID"));//database column -->ChildID
child.setMotherId(resultSet.getString("MotherID"));
child.setFirstName(resultSet.getString("FirstName"));
child.setLastName(resultSet.getString("LastName"));
System.out.println(children);
children.add(child);
System.out.println(children);
}
}
catch (SQLException e) {
e.printStackTrace();
}
finally{
connection.closeConnection();
}
return children;
}
}
请提供您的数据库值 – ScanQR
我该怎么办?只是在这里提到? – Hash
我将需要什么是您执行查询时得到的结果集。另外为什么你的控制器方法是无效的它应该返回更新的模型对象,而不是使用现有的模型。 – ScanQR