下拉选项菜单重复相同的选项

时间:2016-11-12 07:47:33

标签: spring-mvc jdbc

这是我使用spring-mvc实现的。正如我在resultSet中观察到的ChildNameAccess.java正常工作一样。当我把print语句放在这里时,它打印出我真正需要的选项。但是下拉框重复结果集对象中的一个结果。任何人都可以追踪这个并告诉我我的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;
}

}

2 个答案:

答案 0 :(得分:1)

您需要为每个结果集行创建子对象。你不是在创造它。我修好了如下。

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

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()){
            child = new Child();
            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;
}}

答案 1 :(得分:1)

ChildNameAccess.java子项中定义了resultSet迭代之外,尝试每次循环时在循环内创建新的Child对象。因为在Java字符串中是不可变的,而您实际上无法更改字符串的值。这意味着你做child.setFirstName(resultSet.getString("FirstName")); 就像firstName字符串的明智原始值仍然在内存中,并且指向它的其他变量没有改变。

while(resultSet.next()){
                    Child child = new Child();
                    child.setChildId(resultSet.getString("ChildID"));//database column -->ChildID
                    child.setMotherId(resultSet.getString("MotherID"));
                    child.setFirstName(resultSet.getString("FirstName"));
                    System.out.println(resultSet.getString("FirstName"));
                    child.setLastName(resultSet.getString("LastName"));

                    children.add(child);
            }