Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSTL and accessing Beans

Status
Not open for further replies.

Erikxxx

Programmer
May 5, 2003
49
GB
Hi all,

I'm having the following problem when I'm trying to access an array from a bean using JSTL. Can anyone explain for me why this doesn't work. The error Tomcat(5) using JSTL1.0 output is

Unable to find a value for "myList" in object of class "PhoneList" using operator "." (null)

These are the files I'm using

File:tester.jsp
<%@ taglib uri=" prefix="c" %>
<jsp:useBean id="list" class="PhoneList" scope="request"/>

<c:forEach items="${list.myList}" var="entry">
<c:eek:ut value="${entry}";
></c:forEach>


import java.util.*;
public class PhoneList {
ArrayList myList;
public PhoneList()
{
myList = new arrayList();
myList.add("name1");
myList.add("name2");
}

public void setMyList(ArrayList l)
{ myList = l; }

public ArrayList getMyList()
{ return myList;}
}



Thanks
Erik
 
I have a similar problem... i'm a tag lib newbie and i have to understand how pass object from scriptlet <% .. %> world to tablig....

i feel tha tag lib are very clean.. but they are only for bean!?!?!? how can i iterate over a list or even a simply plain array of string object?
i have to exlicit put in context like this?
Code:
pageContext.setAttribute("objectNameForTagLib",objectName);

i also try to instantiate with bean syntax
Code:
<jsp:useBean id="beanName" scope="page"  class="beanClass" />

but it's strictly for bean.. i'd like to instantiate ArrayList, maps Generics and so on... is it possible without scriptlet?
i read that with taglib we have a scriptless jsp... but at this time i don't have a clue on how to do a lot of things with taglibs :(

p.s. i look on google result, on sun's page but i don't find a clear and comprensive taglib tutorial for beginner (or dummies) do you know some!?!?

Thanks in advice
 
Forget my first post.

What about making the list public?

Cheers,
Dian
 
Hi,

Below is the example hope this will help you start with JSTL and JavaBean. Make sure that the JavaBean is in the package.

Java Bean
Code:
package com.test.bean;
import java.util.ArrayList;

public class TestBean 
{
  private ArrayList list = null;

  public TestBean()
  {
    list = new ArrayList();
    list.add("one");
    list.add("two");
    list.add("three");
  }

  public void setList(ArrayList list)
  {
    this.list = list;
  }

  public ArrayList getList()
  {
    return list;
  }
   
}

JSP code

Code:
<%@ taglib uri="[URL unfurl="true"]http://java.sun.com/jstl/core"[/URL] prefix="c"%>
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>JSTL Test</title>
  </head>
  <body>
  This is a simple test for bean using useBean and iterate over the list using JSTL 
  <br>
  <jsp:useBean id="myList" class="com.test.bean.TestBean" scope="page"/>
  
  <c:forEach items="${myList.list}" var="itemValue" varStatus="idx">
   <c:out value="${idx.index}"/>
   &nbsp;&nbsp;&nbsp;
   <c:out value="${itemValue}"/>
   <br>
  </c:forEach>
  </body>
</html>

Hope this will help you.

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top