Spring - 如何使用服务层在JSP表中将数据库内容显示为List?

时间:2016-02-16 08:34:49

标签: java mysql spring jsp

我正在尝试在JSP表中显示List,我不确定我做错了什么。我该怎么做呢?这是一个代码:

PatientDAOImpl:

public List<Patient> getAllpatients() throws SQLException {

    String query = "SELECT * FROM virtualclinic.patient";
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;


        con = dataSource.getConnection();
        ps = con.prepareStatement(query);
        rs = ps.executeQuery();

        List<Patient> patients = new ArrayList<Patient>();

        Patient patient = new Patient();

        while (rs.next()) {

            patient.setId(rs.getString(1));
            patient.setName(rs.getString(2));
            patient.setLastName(rs.getString(3));
            patient.setGender(rs.getString(4));
            patient.setAge(rs.getString(5));
            patient.setPhoneNumber(rs.getString(6));
            patient.setAddress(rs.getString(7));
            patient.setDisease(rs.getString(8));
            patient.setCondition(rs.getString(9));
            patient.setRoomType(rs.getString(10));
            patient.setRoomNumber(rs.getString(11));
            patient.setDate(rs.getString(12));

            patients.add(patient);
        }

        return patients;

ClinicServiceImpl:

public List<Patient> getAllpatients() throws SQLException{
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
    patientDAO = ctx.getBean("patientDAO", PatientDAOImpl.class);

    return patientDAO.getAllpatients();
}

InformationController:

@RequestMapping(value="/informations.html", method = RequestMethod.GET)
public ModelAndView infoPatient(Model model) throws SQLException{

    setAppContext();

    List<Patient> patients = clinicService.getAllpatients();

    model.addAttribute("patients", patients);

    ModelAndView inf = new ModelAndView("InformationsAboutPatient");
    return inf;

}

public void setAppContext(){
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("clinicconfig.xml");
    clinicService = ctx.getBean("clinicService", ClinicServiceImpl.class);
    }

Informations.jsp:

<table border="1">
        <tr style="font-size: 13">
            <td>ID</td>
            <td>First name</td>
            <td>Last name</td>
            <td>Gender</td>
            <td>Age</td>
            <td>Phone number</td>
            <td>Address</td>
            <td>Disease</td>
            <td>Condition</td>
            <td>Room type</td>
            <td>Room number</td>
            <td>Date registration</td>
        </tr>
        <c:forEach var="patient" items="${patients}">
            <tr style="font-size: 10">
                <td>${patient.getId()}</td>
                <td>${patient.getName()}</td>
                <td>${patient.getLastName()}</td>
                <td>${patient.getGender()}</td>
                <td>${patient.getAge()}</td>
                <td>${patient.getPhoneNumber()}</td>
                <td>${patient.getAddress()}</td>
                <td>${patient.getDisease()}</td>
                <td>${patient.getCondition()}</td>
                <td>${patient.getRoomType()}</td>
                <td>${patient.getRoomNumber()}</td>
                <td>${patient.getDate()}</td>
            </tr>
            </c:forEach>
    </table>

clinicconfig.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean id="userDAO" class="org.damian.dao.UserDAOImpl">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="patientDAO" class="org.damian.dao.PatientDAOImpl">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="employeeDAO" class="org.damian.dao.EmployeeDAOImpl">
    <property name="dataSource" ref="dataSource" />
</bean>

   <bean id="clinicService" class="org.damian.service.ClinicServiceImpl">
    <property name="userDAO" ref="userDAO" />
    <property name="patientDAO" ref="patientDAO" />
    <property name="employeeDAO" ref="employeeDAO" />
    <property name="dataSource" ref="dataSource"/>
</bean>

   <mvc:annotation-driven />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3307/virtualclinic" />
     <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

表格中显示的值与<td>形式的值相同。

1 个答案:

答案 0 :(得分:1)

您正在循环中更改相同的Patient对象。您不必获取有关不同患者的信息,而是最终获得List<Patient>中最后一位患者的重复条目。

而不是这样做:

Patient patient = new Patient();
while循环之上

,你应该在while循环中进行。

while (rs.next()) {

    /* Do Here */
    Patient patient = new Patient();

    patient.setId(rs.getString(1));
    patient.setName(rs.getString(2));
    patient.setLastName(rs.getString(3));
    patient.setGender(rs.getString(4));
    patient.setAge(rs.getString(5));
    patient.setPhoneNumber(rs.getString(6));
    patient.setAddress(rs.getString(7));
    patient.setDisease(rs.getString(8));
    patient.setCondition(rs.getString(9));
    patient.setRoomType(rs.getString(10));
    patient.setRoomNumber(rs.getString(11));
    patient.setDate(rs.getString(12));

    patients.add(patient);
}