Spring MVC - 如何在JSP select表单中显示数据库中的所有ID?

时间:2016-01-25 23:43:35

标签: java spring jsp

我在JSP选择表单中显示数据库中的所有ID时出现问题。我该怎么做呢?我尝试做某事,但这段代码不起作用:

Info.jsp:

<select name="id" style="font-size: 20; width:120px; height: 670px" name="id" multiple>
    <c:forEach var="patients" items="patientList">
    <option id="idpatient"></option>
    </c:forEach>
    </select>

InfoController:

@RequestMapping(value="/informations.html", method = RequestMethod.GET)
public ModelAndView infoPatient(@ModelAttribute("idpatient") Patient patient, List<Patient> patientList, Model model) throws SQLException{

    setAppContext();

    clinicService.getAllpatients(patientList, patient);

    model.addAttribute("patients", patientList);

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

}

PatientDAOImpl:

public void getAllpatients(List<Patient> patientList, Patient patient) throws SQLException{

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

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

        while (rs.next()){
            patient.setId(rs.getString(1));

            patientList.add(patient);
        }

ClinicServiceImpl:

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

    patient.setId(patient.getId());
    patientList.add(patient);

    patientDAO.getAllpatients(patientList, patient);

}

这是一个错误:

 SEVERE: Servlet.service() for servlet [spring-dispatcher] in context   with path [/VirtualClinic] threw exception [Request processing failed;  nested exception is org.springframework.beans.BeanInstantiationException:  Failed to instantiate [java.util.List]: Specified class is an interface]  with root cause
 org.springframework.beans.BeanInstantiationException: Failed to  instantiate [java.util.List]: Specified class is an interface
at      org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
 at   org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:80)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:106)

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>SpringJDBCExample</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>

    <!-- Generic properties -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Spring -->
    <spring-framework.version>4.0.2.RELEASE</spring-framework.version>

    <!-- Logging -->
    <logback.version>1.0.13</logback.version>
    <slf4j.version>1.7.5</slf4j.version>

</properties>

<dependencies>

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <!-- Spring and Transactions -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Spring JDBC Support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- MySQL Driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.5</version>
    </dependency>

    <!-- Logging with SLF4J & LogBack -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
        <scope>runtime</scope>
    </dependency>

</dependencies>

1 个答案:

答案 0 :(得分:0)

您正在尝试将List<Patient> 传递到您的控制器,然后进入您的DAO进行填充,然后将其重新启动。这在Java中并不常见; DAO应该创建并返回一个新的List<Patient>,您可以从控制器中删除该参数。

更好的是,不要手写DAO。如果使用JPA没有意义,请使用社区Spring Data JDBC repository,它会自动为您执行DAO操作。