来自控制器的数据未显示在视图JSP页面上

时间:2019-01-30 19:09:49

标签: java spring spring-mvc jsp

我试图使用JSP在数据库中显示从数据库中检索到的数据,但是它们没有显示-即使在控制器上打印出来时也显示了这些数据。您能告诉我问题可能出在哪里吗? (使用Spring MVC)

这是控制器的代码:

@Controller
@RequestMapping("/test")
public class CityController {

// need to inject the city dao
@Autowired
private CityDAO cityDAO;

@GetMapping("/home")
public String showHome() {

    return "home";
}

@GetMapping("/list")
public String listCustomers(Model theModel) {

    // get cities from the dao
    System.out.println(">>>>>>>>> GETING CITIES: ");

    List<City> theCities = cityDAO.getCities();

    System.out.println(" >>>>>>>>> CITIES: " + theCities);

    for (City Cit : theCities) {
        System.out.println("City: " + Cit.getCityName());
    }

    // add the customers to the model
    theModel.addAttribute("cities", theCities);

    return "list-cities";
}
}

这是JSP页面:

<!DOCTYPE html>
<html>

<head>
<title>Cities</title>

<hr>

</head>

<body>

<h2>Cities </h2>

<hr>

        <table>
            <tr>
                <th>City Name</th>
                <th>Country</th>
            </tr>

            <!-- loop over and print our customers -->
            <c:forEach var="tempCustomer" items="${cities}">
                <tr>
                    <td>${tempCustomer.cityName} </td>
                </tr>
            </c:forEach>        
        </table>

 </body>

 </html>

POM文件:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.evotingsystem</groupId>
<artifactId>mycoolwebapp</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>mycoolwebapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>

<springframework.version>5.0.6.RELEASE</springframework.version>
    <hibernate.version>5.2.16.Final</hibernate.version>
    <mysql.connector.version>5.1.45</mysql.connector.version>
    <c3po.version>0.9.5.2</c3po.version>

    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
 </properties>

 <dependencies>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <!-- Add Jackson for JSON converters -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.connector.version}</version>
    </dependency>

    <!-- C3PO -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3po.version}</version>
    </dependency>

    <!-- Servlet+JSP+JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>


    <!-- to compensate for java 9 not including jaxb -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>

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


 </dependencies>
 <build>
 <finalName>mycoolwebapp</finalName>

 <pluginManagement>
        <plugins>
            <plugin>
                <!-- Add Maven coordinates for: maven-war-plugin -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
        </plugins>

    </pluginManagement>
 </build>
 </project>

我得到的输出如下: - $ {tempCustomer.cityName}

1 个答案:

答案 0 :(得分:0)

要在ArrayList框架中迭代JSPSpring MVC上的任何列表,可以使用JSTLhere more info

这里是您的更新代码:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>

<head>
<title>Cities</title>

<hr>

</head>

<body>

<h2>Cities </h2>

<hr>

        <table>
            <tr>
                <th>City Name</th>
                <th>Country</th>
            </tr>

            <!-- loop over and print our customers -->
            <c:forEach var="tempCustomer" items="${cities}">
                <tr>
                    <td>${tempCustomer.cityName} </td>
                </tr>
            </c:forEach>        
        </table>

 </body>

 </html>