You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Antony Paul <an...@hotmail.com> on 2003/12/05 10:38:38 UTC

Calling a bean method in

Hi all,
    How to call a method of a bean which returns an Object from <c:out/>
tag. I have JSTL Standard 1.0 in a Tomcat 4.1.27. I am using commons
beanutils class RowSetDynaClass to display data in JSP pages.
In servlet
List list = rsdc.getRows();
request.setAttribute("list",list);

In JSP I have to code like this
List list = (List) request.getAttribute("list");
Iterator it = list.iterator();
while(it.hasNext()){
   DynaBean bean = (DynaBean) it.next();
    out.print(bean.get("empno"));
}

How to do it using JSTL Standard 1.0.

rgds

Antony Paul


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Antony Paul <an...@hotmail.com>.
Thanks a lot Kris. Now I will use JSTL only.

Antony Paul
----- Original Message -----
From: "Kris Schneider" <kr...@dotech.com>
To: "Tag Libraries Users List" <ta...@jakarta.apache.org>
Sent: Friday, December 05, 2003 10:24 PM
Subject: Re: Calling a bean method in <c:out/>


> Coming in late, but personally, I'd skip the whole RowSetDynaClass thing
and use
> what JSTL already gives you:
>
> import javax.servlet.jsp.jstl.sql.Result;
> import javax.servlet.jsp.jstl.sql.ResultSupport;
> ...
> ResultSet rs = ...;
> Result result = ResultSupport.toResult(rs);
> request.setAttribute("result", result);
> ...
>
> The Result interface is much more JSTL-friendly than DynaBean (which
doesn't
> implement or expose Map).
>
> Quoting Martin van Dijken <su...@windgazer.nl>:
>
> > Hey Antony,
> >
> > You've got it wrong there. The solution for your simple example is this:
> >
> > <%@ page import="java.util.*"%>
> > <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
> > Sample using JSTL Core tags<br>
> > <%
> >   Map map = new HashMap();
> >   map.put("name","Anto Paul");
> >   map.put("Place","Ollur");
> >   map.put("Job","Programmer");
> >   request.setAttribute("list",map);
> > %>
> > <c:forEach var="i" items="${list}">
> >    <c:out value="${i.key}:${i.value}"/><br>
> > </c:forEach>
> >
> > This will print a name:AntoPaul<br>Place:Ollur<br> etc. To get back to
> > your original question, this might be solved if your DynaBeans are
> > instances of Map in the following way:
> >
> > Servlet:
> > List list = rsdc.getRows();
> > request.setAttribute("list",list);
> >
> > JSP:
> > <c:forEach var="i" items="${list}">
> >    <c:out value='${i["empno"]}'/>
> > </c:forEach>
> >
> > Lucky I had a few minutes to spare;) I usually just scream "read the
> > spec":)
> >
> > Martin
>
> --
> Kris Schneider <ma...@dotech.com>
> D.O.Tech       <http://www.dotech.com/>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Kris Schneider <kr...@dotech.com>.
Coming in late, but personally, I'd skip the whole RowSetDynaClass thing and use
what JSTL already gives you:

import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
...
ResultSet rs = ...;
Result result = ResultSupport.toResult(rs);
request.setAttribute("result", result);
...

The Result interface is much more JSTL-friendly than DynaBean (which doesn't
implement or expose Map).

Quoting Martin van Dijken <su...@windgazer.nl>:

> Hey Antony,
> 
> You've got it wrong there. The solution for your simple example is this:
> 
> <%@ page import="java.util.*"%>
> <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
> Sample using JSTL Core tags<br>
> <%
>   Map map = new HashMap();
>   map.put("name","Anto Paul");
>   map.put("Place","Ollur");
>   map.put("Job","Programmer");
>   request.setAttribute("list",map);
> %>
> <c:forEach var="i" items="${list}">
>    <c:out value="${i.key}:${i.value}"/><br>
> </c:forEach>
> 
> This will print a name:AntoPaul<br>Place:Ollur<br> etc. To get back to 
> your original question, this might be solved if your DynaBeans are 
> instances of Map in the following way:
> 
> Servlet:
> List list = rsdc.getRows();
> request.setAttribute("list",list);
> 
> JSP:
> <c:forEach var="i" items="${list}">
>    <c:out value='${i["empno"]}'/>
> </c:forEach>
> 
> Lucky I had a few minutes to spare;) I usually just scream "read the
> spec":)
> 
> Martin

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Martin van Dijken <su...@windgazer.nl>.
Hey Antony,

You've got it wrong there. The solution for your simple example is this:

<%@ page import="java.util.*"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
Sample using JSTL Core tags<br>
<%
  Map map = new HashMap();
  map.put("name","Anto Paul");
  map.put("Place","Ollur");
  map.put("Job","Programmer");
  request.setAttribute("list",map);
%>
<c:forEach var="i" items="${list}">
   <c:out value="${i.key}:${i.value}"/><br>
</c:forEach>

This will print a name:AntoPaul<br>Place:Ollur<br> etc. To get back to 
your original question, this might be solved if your DynaBeans are 
instances of Map in the following way:

Servlet:
List list = rsdc.getRows();
request.setAttribute("list",list);

JSP:
<c:forEach var="i" items="${list}">
   <c:out value='${i["empno"]}'/>
</c:forEach>

Lucky I had a few minutes to spare;) I usually just scream "read the spec":)

Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Antony Paul <an...@hotmail.com>.
I tried this it gives error.
/jstl1.jsp(12,25) equal symbol expected
<%@ page import="java.util.*"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
Sample using JSTL Core tags<br>
<%
 Map map = new HashMap();
 map.put("name","Anto Paul");
 map.put("Place","Ollur");
 map.put("Job","Programmer");
 request.setAttribute("list",map);
%>
<c:forEach var="i" items="${list}">
  <c:out value="${i["name"]}"/><br>
</c:forEach>

rgds
Antony Paul


----- Original Message -----
From: "Antony Paul" <an...@hotmail.com>
To: "Tag Libraries Users List" <ta...@jakarta.apache.org>
Sent: Friday, December 05, 2003 3:53 PM
Subject: Re: Calling a bean method in <c:out/>


> Then any other way to do it using JSTL or Jakarta taglibs. This
functionaly
> is very useful because most pages uses the same technique to display
> content. I think Struts is providing similar functionality.
>
> Antony Paul.
>
> ----- Original Message -----
> From: "Martin van Dijken" <su...@windgazer.nl>
> To: "Tag Libraries Users List" <ta...@jakarta.apache.org>
> Sent: Friday, December 05, 2003 3:37 PM
> Subject: Re: Calling a bean method in <c:out/>
>
>
> > Hey Antony,
> >
> > It is correct that that gives an error. You see it is impossible to call
> >   methods in the Expression Language of JSTL. If your bean-class
> > implements java.util.Map however, it is possible to get the item you
> > want by using:
> >
> > <c:forEach var="i" items="${list}">
> >   <c:out value="${i["empno"]}"/><br>
> > </c:forEach>
> >
> > Grtz,
> >
> > Martin
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Antony Paul <an...@hotmail.com>.
Then any other way to do it using JSTL or Jakarta taglibs. This functionaly
is very useful because most pages uses the same technique to display
content. I think Struts is providing similar functionality.

Antony Paul.

----- Original Message -----
From: "Martin van Dijken" <su...@windgazer.nl>
To: "Tag Libraries Users List" <ta...@jakarta.apache.org>
Sent: Friday, December 05, 2003 3:37 PM
Subject: Re: Calling a bean method in <c:out/>


> Hey Antony,
>
> It is correct that that gives an error. You see it is impossible to call
>   methods in the Expression Language of JSTL. If your bean-class
> implements java.util.Map however, it is possible to get the item you
> want by using:
>
> <c:forEach var="i" items="${list}">
>   <c:out value="${i["empno"]}"/><br>
> </c:forEach>
>
> Grtz,
>
> Martin
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Martin van Dijken <su...@windgazer.nl>.
Hey Antony,

It is correct that that gives an error. You see it is impossible to call 
  methods in the Expression Language of JSTL. If your bean-class 
implements java.util.Map however, it is possible to get the item you 
want by using:

<c:forEach var="i" items="${list}">
  <c:out value="${i["empno"]}"/><br>
</c:forEach>

Grtz,

Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Antony Paul <an...@hotmail.com>.
Thank you for the reply.
I am new to JSTL. I tried to use it like this in JSP
<c:forEach var="i" items="${list}">
 <c:out value="${i.get("empno")}"/><br>
</c:forEach>

But it gives error
org.apache.jasper.JasperException: /TestDB.jsp(48,29) equal symbol expected


Antony Paul

----- Original Message -----
From: "Martin van Dijken" <su...@windgazer.nl>
To: "Tag Libraries Users List" <ta...@jakarta.apache.org>
Sent: Friday, December 05, 2003 3:11 PM
Subject: Re: Calling a bean method in <c:out/>


> Hey Antony,
>
> Try using the <c:forEach> tag. It loops each item of lists, arrays etc.
>
> Martin
>
> Antony Paul wrote:
> > Hi all,
> >     How to call a method of a bean which returns an Object from <c:out/>
> > tag. I have JSTL Standard 1.0 in a Tomcat 4.1.27. I am using commons
> > beanutils class RowSetDynaClass to display data in JSP pages.
> > In servlet
> > List list = rsdc.getRows();
> > request.setAttribute("list",list);
> >
> > In JSP I have to code like this
> > List list = (List) request.getAttribute("list");
> > Iterator it = list.iterator();
> > while(it.hasNext()){
> >    DynaBean bean = (DynaBean) it.next();
> >     out.print(bean.get("empno"));
> > }
> >
> > How to do it using JSTL Standard 1.0.
> >
> > rgds
> >
> > Antony Paul
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Calling a bean method in

Posted by Martin van Dijken <su...@windgazer.nl>.
Hey Antony,

Try using the <c:forEach> tag. It loops each item of lists, arrays etc.

Martin

Antony Paul wrote:
> Hi all,
>     How to call a method of a bean which returns an Object from <c:out/>
> tag. I have JSTL Standard 1.0 in a Tomcat 4.1.27. I am using commons
> beanutils class RowSetDynaClass to display data in JSP pages.
> In servlet
> List list = rsdc.getRows();
> request.setAttribute("list",list);
> 
> In JSP I have to code like this
> List list = (List) request.getAttribute("list");
> Iterator it = list.iterator();
> while(it.hasNext()){
>    DynaBean bean = (DynaBean) it.next();
>     out.print(bean.get("empno"));
> }
> 
> How to do it using JSTL Standard 1.0.
> 
> rgds
> 
> Antony Paul
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org