List of objects + getters + c:forEach
Hi,
Thank you for reading my post.
Suppose I have the following Java class:
Code:
package com.test;
public class Person
{
private String ms_fstName = null;
private String ms_name = null;
public Person(String s_fstName, String s_name)
{
ms_fstName = s_fstName;
ms_name = s_name;
}
public String getMs_fstName()
{
return ms_fstName;
}
public String getMs_name()
{
return ms_name;
}
}
Now, suppose I have the following JSP:
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.test.Person"%>
<%@page import="java.util.ArrayList"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL tests</title>
</head>
<body>
<%
Person p0 = new Person("Jane", "Deejena");
Person p1 = new Person("Antonin", "Brontu");
Person p2 = new Person("Jojo", "Latortue");
ArrayList<Person> persons = new ArrayList<Person>();
persons.add(0, p0);
persons.add(1, p1);
persons.add(2, p2);
%>
<c:forEach items="persons" var="person">
<c:out value="${person.getMs_fstName()} "></c:out>
<c:out value="${person.getMs_name()}"></c:out>
<br />
</c:forEach>
</body>
</html>
Basically, I just want to print a list of persons' first name and name.
But the above code doesn't work (nothing is printed).
Can you tell me what I do wrong and how to do things right?
I suspect putting Java code inside JSTL XML elements attributes values is not correct but I don't know what I should do instead...
Thank you for helping and best regards,
--
Lmhelp