Results 1 to 5 of 5
  1. #1
    Lmhelp is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Question List of objects + getters + c:forEach

    Hi,

    Thank you for reading my post.

    Suppose I have the following Java class:
    Java 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:
    Java 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

  2. #2
    Lmhelp is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Post

    I made a change in the "Person" class (in blue) and corrected an error (in red) in the JSP.

    Change in the Person class:
    Java Code:
    Before: [COLOR="Blue"]private [/COLOR]String ms_fstName = null;
    After:  [COLOR="Blue"]public [/COLOR]String ms_fstName = null;
    Correcting in the JSP:
    Java Code:
    Before:
    <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>
    After:
    <c:forEach items="[COLOR="Red"]${[/COLOR]persons[COLOR="Red"]}[/COLOR]" var="person">
    			<c:out value="${person.ms_fstName} "></c:out>
    			<br />
    </c:forEach>
    The persons' first names are printed.

    Now,if I add the line in red below, I have an error:
    Java Code:
    <c:forEach items="${persons}" var="person">
    			<c:out value="${person.ms_fstName}"></c:out>	
    			[COLOR="Red"]<c:out value="${person.getMs_name()}"></c:out>[/COLOR]
    			<br />
    </c:forEach>
    The error:
    Java Code:
    SEVERE: Servlet.service() for servlet jsp threw exception
    org.apache.jasper.JasperException: /jstl_tests.jsp(36,3) The function getMs_name must be used with a prefix when a default namespace is not specified
    Can you tell me what you think about that?

    Thanks and best regards,
    --
    Lmhelp

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    ))
    Why did you decide to use the person.ms_fstName instead person.getMs_fstName how you use for two statement?
    Skype: petrarsentev
    http://TrackStudio.com

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default

    Java Code:
    <c:out value="${person.ms_fstName}"></c:out>
    This works because this is proper EL.
    It will take the attribute name and convert it into a getMs_fstName() call.

    Make the attribute private again.

    And you need to look at your naming convention. It's seriously non-standard.

  5. #5
    Lmhelp is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you for your answers.

    I made the member "ms_fstName" private again as "Tolls" suggested
    and indeed it still works (I think this answers "Petr"'s question).

    Thank you and best regards,
    --
    Lmhelp

Similar Threads

  1. <c:forEach to list data in X columns
    By polygrimm in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-04-2009, 09:27 PM
  2. add objects to list
    By javaMike in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 11:11 PM
  3. List of objects
    By bubbless in forum New To Java
    Replies: 0
    Last Post: 08-01-2009, 05:21 PM
  4. List of objects
    By rekha in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 12:39 PM
  5. Getting objects from a list
    By markyoung1984 in forum New To Java
    Replies: 4
    Last Post: 03-13-2008, 11:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •