在Spring MVC Web应用程序中使用JSTL显示列表



.jsp

 <c:forEach items = "${allContacts}" var="contact">
            <c:out value="${contact.firstName}"/>,
            <c:out value="${contact.lastName}"/>
     </c:forEach>

联系服务.java

  public static List listContacts() {
        return toList(contacts);
    }
    private static List toList(Map contacts) {
        List contactList = new ArrayList();
        Iterator iterator = contacts.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            Contact contact = (Contact) entry.getValue();
            contactList.add(contact);
        }
        return contactList;
    }

列表联系人控制器.java

public class ListContactsController extends AbstractController {
    public ListContactsController() {
    }
    public ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        List allContacts = ContactService.listContacts();
        return new ModelAndView("ListContacts","allContacts", allContacts);
    }
}

我正在尝试列出allContacts,但仅显示","作为输出。数据显示在 GET 中,但不显示在网页中。问题出在哪里?

您可能

没有在控制器中添加allContacts对象:

session.setAttribute("allContacts", allContactsVariable);

@Ralph的评论:

由于打印出逗号,这意味着列表中存在对象,但这些对象的字段中没有值。

您是否尝试过在 GET 之后添加值? 例如,contact1.setFirstName("value");contact.setLastName("value")

最新更新