JSP无法从spring控制器打印列表

时间:2016-03-29 11:09:44

标签: spring list jsp

我想从我的数据库打印联系人列表,但是我的jsp视图无法打印它。

this is contact view

这是控制器

@RequestMapping(value="contact")
public ModelAndView listContact(ModelAndView model) throws IOException{
    List<contactModel> listContact = contactDAO.listContact();
    model.addObject("listContact", listContact);
    model.setViewName("contact");
    for(contactModel contact : listContact){
        System.out.println(contact.getCid()+", "+contact.getUid()+", "+contact.getName());
    }
    return model;
}

这是视图代码

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Contact Manager Home</title>
    </head>
    <body>
        <div align="center">
            <h1>Contact List</h1>
            <h3><a href="newContact">New Contact</a></h3>
            <table border="1">
                <th>No</th>
                <th>Name</th>
                <th>Email</th>
                <th>Address</th>
                <th>Telephone</th>
                <th>Action</th>

                <c:forEach var="contact" items="${ListContact}" varStatus="status">
                <tr>
                    <td>${status.index + 1}</td>
                    <td>${contact.name}</td>
                    <td>${contact.email}</td>
                    <td>${contact.address}</td>
                    <td>${contact.phone}</td>
                    <td>
                        <a href="editContact?id=${contact.id}">Edit</a>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="deleteContact?id=${contact.id}">Delete</a>
                    </td>           
                </tr>
                </c:forEach>                
            </table>
        </div>
    </body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>Reminder and Address Book</display-name>
  <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.hbj.raddb.config</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>  
</web-app>

联系人值只能在控制台中打印,但不能打印到jsp视图。我的错在哪里?有人可以帮我解决问题吗?

2 个答案:

答案 0 :(得分:2)

你有一个错字,改变:

<c:forEach var="contact" items="${ListContact}" varStatus="status">

到:

<c:forEach var="contact" items="${listContact}" varStatus="status">

答案 1 :(得分:0)

您必须使用<c:out>包围这些变量。例如:

<td><c:out value="${status.index + 1} /></td>