這是我使用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